子类也可以有子类,ed就是子类的子类,也可以是a的子类
package com.extend_test01;public class Extends {public static void main(String[] args) {Pupil pupil = new Pupil();pupil.setName("xiao");pupil.setScore(60);pupil.tesing();System.out.println("===");University university = new University();university.setName("mei");university.setScore(100);university.tesing();}
}
//父类,同对象数据同方法放置期中
package com.extend_test01;public class Student {String name;int age;double score;public String getName() {return name;}public void setName(String name) {this.name = name;System.out.println(name+"正在考试");}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}}
//子类,不同对象数据和方法放入相应子类
package com.extend_test01;public class Pupil extends Student{public void tesing(){System.out.println("小学生"+name+"成绩"+score);}
}
package com.extend_test01;public class University extends Student{public void tesing(){System.out.println("大学生"+name+"成绩"+score);}
}
package com.extend_test02;public class Father {public int n1=100;private int n2=200;//创建一个公共的方法,直接返回n2的值,此时想在非同类使用n2的值只需要用这个方法即可public int getn2(){return n2;}public void test(){System.out.println("meimeide");}private void test2(){System.out.println("choub");}
}
。//子类
package com.extend_test02;public class Son extends Father{public void sou(){
// System.out.println(n1+n2);直接访问是不行的System.out.println(n1+" "+getn2());}}
当父类无默认构造器时,子类的每个构造器都需要用super指定使用的父类构造器
super只能用在构造器
第四点对应案列:
父类。有三个构造器
子类构造器要调用谁就用对应的super(参数);
、继承在jvm内存的展示:
先在方法区加载继承顺序,再在堆分配空间。先从顶级父类object开始
按照此规则,Son son =new Son();son.name为大头儿子,son.age为39son.hobby为旅游。
小测试题
因为super和this在构造器只能有一个,所以,B(){}中有this就不会默认有super访问父类A的默认构造器了,直接来到B(String name){}构造器,由于该构造器无this,且每个构造器都默认有个super访问父类的默认构造器,所以直接来到了A的默认构造器,输出a,然后再回到B(String name){}构造器输出b name 再回到B(){}输出b
package com.extend_test03;
class Test{public static void main(String[] args) {Pc pc = new Pc(15,"jsjd",235,"xiaomi");pc.Printf();NotePad notePad = new NotePad(15,"jsjd",235,"xiaomi");notePad.Printf();}
}public class Computer {int CPU;String hard_disk;double memory;public Computer(int cpu,String hard_,double memory)//构造器,初始化数据{this.CPU=cpu;this.memory=memory;this.hard_disk=hard_;}public String getDetail()//返回值{return"cpu="+CPU+"hard="+hard_disk+"memory="+memory;}
//封装public int getCPU() {return CPU;}public void setCPU(int CPU) {this.CPU = CPU;}public String getHard_disk() {return hard_disk;}public void setHard_disk(String hard_disk) {this.hard_disk = hard_disk;}public double getMemory() {return memory;}public void setMemory(double memory) {this.memory = memory;}
}
//子类Pc
class Pc extends Computer{String Brand;//多的属性public Pc(int cpu, String hard_, double memory, String brand) {//构造器super(cpu, hard_, memory);//由于父类有个有属性的构造器,则,,默认构造器没有了,此时指定需要的构造器Brand = brand;}
//封装public String getBrand() {return Brand;}public void setBrand(String brand) {Brand = brand;}public void Printf(){System.out.println(getDetail()+"品牌"+getBrand());}
}
class NotePad extends Computer{String color;public NotePad(int cpu, String hard_, double memory, String color) {super(cpu, hard_, memory);this.color = color;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public void Printf(){
//getDetail为父类方法,要打印数值,可直接在子类使用该方法再加上该子类独有的属性System.out.println(getDetail()+"颜色"+getColor());}
}