版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、3、类的高级特性(抽象类、接口等)引入:本章继续深入学习面向对象思想。正课:1、抽象类 并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。用abstract修饰。l 定义抽象类:abstract class 类名称 成员变量; 方法(); /定义一般方法 abstract 方法(); /定义抽象方法 范例testabstract:package testabstract;public abstract class Animal public Animal(String aType) type = new String(aType); pu
2、blic String toString() return "This is a " + type; / Dummy method to be implemented in the derived classes public abstract void sound(); private String type;package testabstract;public class Cat extends Animal public Cat(String aName) super("Cat"); / Call the base constructor nam
3、e = aName; / Supplied name breed = "Unknown" / Default breed value public Cat(String aName, String aBreed) super("Cat"); / Call the base constructor name = aName; / Supplied name breed = aBreed; / Supplied breed / Return a String full of a cat's details public String toString
4、() return super.toString() + "nIt's " + name + " the " + breed; / A miaowing method public void sound() System.out.println("Miiaooww"); private String name; / Name of a cat private String breed; / Cat breedpackage testabstract;public class Dog extends Animal public
5、Dog(String aName) super("Dog"); / Call the base constructor name = aName; / Supplied name breed = "Unknown" / Default breed value public Dog(String aName, String aBreed) super("Dog"); / Call the base constructor name = aName; / Supplied name breed = aBreed; / Supplied b
6、reed / Present a dog's details as a string public String toString() return super.toString() + "nIt's " + name + " the " + breed; / A barking method public void sound() System.out.println("Woof Woof"); private String name; / Name of a Dog private String breed; /
7、Dog breedpackage testabstract;public class Duck extends Animal public Duck(String aName) super("Duck"); / Call the base constructor name = aName; / Supplied name breed = "Unknown" / Default breed value public Duck(String aName, String aBreed) super("Duck"); / Call the b
8、ase constructor name = aName; / Supplied name breed = aBreed; / Supplied breed / Return a String full of a duck's details public String toString() return super.toString() + "nIt's " + name + " the " + breed; / A quacking method public void sound() System.out.println("
9、;Quack quackquack"); private String name; / Duck name private String breed; / Duck breedpackage testabstract;class Spaniel extends Dog public Spaniel(String aName) super(aName, "Spaniel"); package testabstract;import java.util.Random;public class TestAbstract public static void main(S
10、tring args) / Create an array of three different animals Animal theAnimals = new Dog("Rover", "Poodle"), new Cat("Max", "Abyssinian"), new Duck("Daffy","Aylesbury"), new Spaniel("Fido") ; Animal petChoice; / Choice of pet Random s
11、elect = new Random(); / Random number generator / Make five random choices of pet for(int i = 0; i < 5; i+) / Choose a random animal as a pet petChoice = theAnimalsselect.nextInt(theAnimals.length); System.out.println("nYour choice:n" + petChoice); petChoice.sound(); / Get the pet's
12、 reaction 2、接口接口可以被用来实现类间多继承结构。接口内部只能定义 public 的抽象方法和静态的、公有常量,因此所有的方法需要在子类中实现。接口定义方法:l 访问权限 interface 接口名称 extends 父接口名1,父接口名2,. /接口体程序 范例testinterface:package testinterface;public class Cat implements PetOutput public Cat(String aName) /super("Cat"); / Call the base constructor name = aNa
13、me; / Supplied name breed = "Unknown" / Default breed value public Cat(String aName, String aBreed) /super("Cat"); / Call the base constructor name = aName; / Supplied name breed = aBreed; / Supplied breed / Return a String full of a cat's details public String toString() ret
14、urn "nIt's " + name + " the " + breed; / A miaowing method public void sound() System.out.println("Miiaooww"); private String name; / Name of a cat private String breed; / Cat breedpackage testinterface;public class Dog implements PetOutput public Dog(String aName)
15、name = aName; / Supplied name breed = "Unknown" / Default breed value public Dog(String aName, String aBreed) name = aName; / Supplied name breed = aBreed; / Supplied breed / Present a dog's details as a string public String toString() return "It's " + name + " the &
16、quot; + breed; / A barking method public void sound() System.out.println("Woof Woof"); public String getName() return name; private String name; / Name of a dog private String breed; / Dog breedpackage testinterface;public class Duck implements PetOutput public Duck(String aName) /super(&q
17、uot;Duck"); / Call the base constructor name = aName; / Supplied name breed = "Unknown" / Default breed value public void layEgg() System.out.println("Egg laid"); public Duck(String aName, String aBreed) /super("Duck"); / Call the base constructor name = aName; / S
18、upplied name breed = aBreed; / Supplied breed / Return a String full of a duck's details public String toString() return "nIt's " + name + " the " + breed; / A quacking method public void sound() System.out.println("Quack quackquack"); private String name; / Duc
19、k name private String breed; / Duck breedpackage testinterface;public interface PetOutput void sound();package testinterface;public interface Read1 void readNewspaper();package testinterface;/import testabstract.Read;class Spaniel extends Dog implements Read1 public Spaniel(String aName) super(aName
20、, "Spaniel"); public void readNewspaper( ) System.out.println("I can read Newspaper"); package testinterface;import java.util.Random;public class TestInterface public static void main(String args) / PetOutput thePets = / Dog("Rover", "Poodle"), / new Cat("Max", "Abyssini
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年浙江单招考前预测密卷含答案文化素质职业技能双科综合
- 2026年福建单招健康管理大类智慧健康管理职业适应性题库含答案
- 2026年黑龙江单招会计专业中职生技能高频题含答案含账务处理
- 2026年陕西单招语文应用文写作专项通知书信倡议书经典题
- 2026年重庆单招工业机器人专业中职生技能经典题含编程基础
- 2026年山西单招数字经济大类直播电商与内容运营技能模拟卷含答案
- 2026年上海单招服装设计专业面试经典题含作品解读
- 2026年内蒙古单招免考加分项配套练习题含答案政策适配版
- 2026年天津单招交通运输类职业适应性高频题含答案含铁道常识
- 2026年新疆单招动物医学专业技能模拟卷含答案含疾病诊断
- 购车合伙协议书模板
- 2025年《道路运输安全培训》知识考试题库及答案解析
- 充电宝产品设计开发全流程
- 院内感染暴发应急响应全流程
- caac机长证考试内容
- 转移性副神经节瘤和嗜铬细胞瘤诊治专家共识2026
- 2025年秋小学音乐湘艺版四年级上册期末测试卷含答案
- 2025年山东省考公务员面试题(监狱警察)及解析
- 国家公园休闲管理
- 2025年教师招聘考试教育综合知识6000题(主观题含答案)
- 基于生成对抗网络的图像修复与超分辨率-洞察及研究
评论
0/150
提交评论