200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Java面向对象 继承 super 方法重写

Java面向对象 继承 super 方法重写

时间:2018-09-25 04:10:31

相关推荐

Java面向对象 继承 super 方法重写

/*继承:表示父类跟子类之间的关系当两个类或者多个类具备相同的属性和方法的时候,可以提取出来,变成父类,子类可以继承子类跟父类是is-a的关系使用:1.使用继承的时候需要使用extend关键字2.使用继承关系之后,父类中的属性和方法都可以在子类中进行使用(非私有属性和非私有方法)3.java中是单继承关系(如果包含多个父类,同时父类中包含重名方法,无法决定该调用谁)super: 是直接父类 对象的引用用途:1.可以在子类中调用父类中被子类覆盖的方法 super.父类方法名称2.当super在普通方法中使用的话,可以任意位置编写3.当super在构造方法中使用的话,会调用父类的构造方法,一定要将super放在第一行4.在构造方法中super关键字和this关键字(指的是不能同时使用this关键字调用重载的构造方法)不能同时出现5.父类中私有的属性和方法都不能被调用,包括构造方法6.子类的构造方法中都会默认使用super关键字调用父类的无参构造方法,因此在定义类的时候,无论自己是否自定义了其他构造方法,最好将无参构造方法写上。7.如果构造方法中显示的指定了super的构造方法,那么无参的构造方法就不会被调用总结:1.在创建子类对象的时候一定会优先创建父类对象2.所有的java类都具备同一个老祖宗类,称为Object,是所有类的根类重写:(覆盖)@override必须要存在继承关系,当父类中的方法无法满足子类需求的时候可以选择使用重写的方式注意:1.重写表示的是子类覆盖父类的方法,当覆盖之后,调用同样的方法的时候会优先调用子类2.重写的方法名称,返回值类型,参数列表必须跟父类一致3.子类重写的方法不允许比父类的方法具备更小的访问权限父类 public 子类 public父类 protected子类 public protected父类 default子类 public protected default父类的静态方法子类可以进行调用,但是子类不可以重写*/

public class Dog extends Pet {// private String name;// private int age;// private String gender;private String sound;public Dog(){}public Dog(String name){super(name);}public Dog(String name, int age, String gender, String sound) {super(name,age,gender);//调用Pet中的构造器// this.name = name;// this.age = age;// this.gender = gender;this.sound = sound;}// public String getName() {//return name;// }// public void setName(String name) {// this.name = name;// }// public int getAge() {//return age;// }// public void setAge(int age) {// this.age = age;//}// public String getGender() {// return gender;// }// public void setGender(String gender) {//this.gender = gender;// }public String getSound() {return sound;}public void setSound(String sound) {this.sound = sound;}public void show(){// System.out.println("name:"+this.name +"age:"+this.age+"gander:"+this.gander+"sount:"+this.sound);System.out.println("name:"+this.getName()+"age:"+this.getAge()+"gender:"+this.getGender()+"sount:"+this.sound);}@Override//重写public void print(){System.out.println("Dog print .....");//super.print();}}

public class Penguin {private String name;private int age;private String gender;private String color;public Penguin(){}public Penguin(String name, int age, String gender, String color) {this.name = name;this.age = age;this.gender = gender;this.color = color;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public void show(){System.out.println("name:"+this.name +"age:"+this.age+"gender:"+this.gender+"color:"+this.color);}}

public class Pet {private String name;private int age;private String gender;public Pet(){super();//指向顶级父类Object// System.out.println("空参");}public Pet(String name ){this.name = name;}public Pet(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public void print(){System.out.println("Pet print......");}}

public class PetTest {public static void main(String[] args) {/* Dog dog = new Dog();dog.setName("白");dog.setAge(10);dog.setGender("雄性");dog.setSound("汪汪..");// dog.show();dog.print();*/Dog dog = new Dog("小白",10,"雄性","旺旺... ...");//dog.print();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。