0
点赞
收藏
分享

微信扫一扫

继承、多态和抽象类

ZGtheGreat 2022-08-16 阅读 88


继承以及多态:

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<ps.length;i++){
System.out.println(ps[i].toString());
}
System.out.println("---------------");
}

/*public static void print(Person[] ps){
for(int i=0; i<ps.length;i++){
System.out.println(ps[i].toString());
}
System.out.println("---------------");
}*/

public static int indexOf(Object objs[],String name){
for(int i=0;i<objs.length;i++){
if(!(objs[i] instanceof Person)){ //反逻辑,卫条件
continue;
}
Person p=(Person) objs[i];
if(p.getName().equals(name)){
return i;
}
}
return -1;
}
public static Object[] merge(Object objs1[],Object objs2[]){
Object objs[] = new Object[objs1.length+objs2.length];
for(int i=0;i<objs1.length;i++){
objs[i]=objs1[i];
}
for(int i=0;i<objs2.length;i++){
objs[objs1.length+i]=objs2[i];
}
return objs;
}
}



//第二种情况


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);
}
}

抽象类部分:

//如果一个类中存在抽象方法,那么该类必须声明成抽象类,,
//一个抽象类是不能实例化---即不能new对象
//抽象类一般只是当父类用的,,利用多态特性,用父类引用调用子类实现

public abstract class CloseFiguer {
public abstract double perimeter();
public abstract double area();
public String toString(){
return "周长:"+perimeter()+",面积:"+area();//方法的前面省略了this.
}

}

//一个类继承了抽象父类,那么必须实现(覆盖父类的抽象方法)抽象父类中的所有抽象方法,
//否则就要声明为抽象类
public class Rcetangle extends CloseFiguer {
private int width;
private int height;
public Rcetangle(int width,int height){
this.width=width;
this.height=height;
}
public double perimeter(){
return (width+height)*2;
}
public double area(){
return width*height;
}
}


public class Run {

public static void main(String[] args) {
//CloseFiguer g = new CloseFiguer()://WA:抽象类不能实例化
CloseFiguer g = new Rcetangle(10,20);
System.out.println( g.toString() );//1.编译时多态:到CloseFigure类中查找看是否有toString()方法,,2.运行时多态:到所new的类即Rectangle中查找并调用toString()方法
System.out.println("area:"+ g.area() ); //1.编译时多态:由CloseFigure类中的抽象方法area()解决,,2.运行时多态:到所new的类即Rectangle中查找并调用area()方法
}
}

举报

相关推荐

0 条评论