继承、多态和抽象类

网友投稿 492 2022-10-18

继承、多态和抽象类

继承、多态和抽象类

继承以及多态:

public class MyDate { private int year; private int month; private int day; public MyDate(int year,int month,int day){ this.year = year; this.month = month; this.day = day; } public MyDate(){ this(1970,01,01); } public MyDate (MyDate d){ this(d.year,d.month,d.day); } public void setYear(int year){ this.year=year; } public int getYear(){ return year; } public String toString(){ return year+"年"+month+"月"+day+"日"; } }

public class Person { private String name; private MyDate birth; public Person(String name,MyDate birth){ this.name=name; this.birth=birth;//new MyDate(birth);//深拷贝 } public Person(){ this("NONAME",null); } public Person(Person p){ this(p.name,p.birth); } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setBirth(MyDate birth){ this.birth=birth; } public MyDate getBirth(){ return birth; } public String toStriing(){ return name+","+birth.toString(); } //解决DouTai2.java第22行中的编译时多态 public void a(){ } public int oldThan(Person p){ return p.birth.getYear()-this.birth.getYear(); }}

public class Student extends Person { private String spec; public Student(String name,MyDate birth,String spec){ super(name,birth);// 明明定义了name,birth 为什么说没有定义呢? this.spec=spec; } public Student(){ this("NONAME",new MyDate(),"Geme"); } public String getSpec(){ return spec; } public void setSpec(String spec){ this.spec=spec; } public String toString(){ return super.toString()+","+spec; } public void aa(){ System.out.println("aaaaa....."); } }

public class Group { public static void main(String[] args) { Person[] table={ new Person("李小明",new MyDate(1996,1,1)), new Student("张小莉",new MyDate(1997,10,5),"Java"), new Student("孙小四",new MyDate(2017,1,26),"C++") }; print(table); Object objs[]={ new Person("李小明",new MyDate(1996,1,1)), new MyDate(2009,11,20), new Student("张小莉",new MyDate(1997,10,5),"Java"), new Student("孙小四",new MyDate(2017,1,26),"C++") }; print(objs); String name="张小莉"; int idx=indexOf(table,name);//indexOf(objs,name) if(idx!=-1){ System.out.println("找到了:"+table[idx]); }else{ System.out.println("没找到:"+name); } System.out.println("++++++++++++"); Object arr[]=merge(table,objs); print(arr); } public static void print(Object[] ps){ for(int i=0; i

//第二种情况

public class DouTai2 { public static void main(String[] args) { Student s = new Student("Jack",new MyDate(2017,4,26),"Java"); System.out.println(s); boolean boo=s instanceof Person;//子类对象及时父对象--学生即是人 System.out.println("1:"+boo);//true Person p = new Person("Tom",new MyDate(2008,8,8)); boolean boo2= p instanceof Student; System.out.println("2:"+boo2); //最经典的多态,下面一句中,左边决定编是多态即通过PP所调用的东西都必须在Person类中存在, //右边决定运行时多态即通过pp所调用的语句在执行时先从Student类中进行查找执行 Person pp = new Student("Rose",new MyDate(2017,1,1),"c++"); System.out.println("pp:"+pp.toString());//运行时多态决定,从Student类中查找 /* 多态包含两个步骤: 1.编译时多态(javac)----看类型,,,看引用变量的类型所对应类(包括父类)中是否有要调用的方法----方法名+参数类型列表 2.运行时多态(java)----看内存,,new谁调谁(从所new的类开始查找,若有就调,否则往它的父查找,以此类推!) -Object中的所有方法都是多态方法 */ pp.a(); //Student s2=new Person;//WA,原因:父类对象不一定是子类对象 int n1 = s.oldThan(p); System.out.println("s比p大的岁数:"+n1); int n2=p.oldThan(s); System.out.println("p比s大的岁数:"+n2); int n3=pp.oldThan(s); System.out.println("pp比s大的岁数:"+n3); }}

抽象类部分:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDOJ--2001 计算两点间的距离 + HDOJ--2002 计算球体积
下一篇:Smart Location Library:简化定位程序使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~