Java object-oriented inheritance of (a)

Java's object-oriented inheritance

Inheritance is the second largest object-oriented features, is an important means to achieve software reuse, and it is the cornerstone of object-oriented. The table of contents will take over around inherit this feature to expand exploration.

introduction

The concept of inheritance, there is in our lives.

  • It is, for example, Pharaoh and his son Wang is the relationship between father and son, after Wang to inherit the mantle of Pharaoh!
  • Obviously, Wang and Wang, like many respects, have the same last name, same address ......
  • Wang Church Wang a lot of truth and acted, Wang Wang emulate in many aspects of behavior.
  • Pharaoh love to drink, Wang does not like, but like Coke, Wang's son Wang little like drinking Sprite.
  • Indeed, ethical, then, Wang can not have two fathers, but Pharaoh could have two sons.

Liu is a good friend of Pharaoh, Pharaoh through the old Liu will be able to know many of the characteristics of Wang ......
ha ha ha, said a lot, which many say is analogous to the Java inheritance.
The following program, in fact, slightly wrong, because the pattern is too small when the class is created. . But they understand it will be relatively easier, follow-up will be perfect examples.

package com.my.pac11;

/**
* @author Summerday
* @date 2019/12/9 19:45
*/
//老王类
public class LaoWang {
   private String lastName;//姓氏
   public String address;//家庭住址
   //super类构造器
   public LaoWang() {
       System.out.println("未知信息老王创建");
   }

   public LaoWang(String address) {
       this.address = address;
       System.out.println("创建家住在" + this.address + "王xx");
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public String getLastName() {
       return lastName;
   }


   public void doGoodThings() {
       System.out.println(this.getLastName() + "做好事……");
   }

   public void drink() {
       System.out.println("老王酗酒……");
   }
   //重写Object类的toString方法
   public String toString(){
       return "Object类是所有类的直接父类或间接父类";
   }
}

//老李类
class LaoLi {
   public LaoLi() {
       System.out.println("我是老李!");
   }
}
//LaoWang 是LittleXiaoWang的间接父类
class LittleXiaoWang extends XiaoWang {
   //没有构造器会报错,子类必须第一步调用父类构造器
   LittleXiaoWang(String n, String a) {
       super(n, a);
   }
   //重写XiaoWang的方法,继承谁就改谁的
   public void drink() {
       System.out.println(this.getLastName() + "不喜欢喝可乐,喜欢雪碧……");
   }
}
//一个类最多只有一个直接父类 ,可以有很多间接父类
//false: class XiaoWang extends LaoLi,LaoWang{...}
class XiaoWang extends LaoWang {
   //子类构造器
   XiaoWang(String lastName, String address) {
       //super调用父类构造器
       super(address);
       setLastName(lastName);
   }

   //重写父类的方法
   public void drink() {
       System.out.println(this.getLastName() + "不酗酒,喜欢喝可乐……");
   }

   public static void main(String[] args) {
       //先调用父类适合的构造器
       //创建家住在浙江王xx
       XiaoWang xw = new XiaoWang("小王", "浙江");
       //false:System.out.println(l.lastName);
       System.out.println(xw.getLastName() + "," + xw.address);//小王,浙江
       //做好事……
       xw.doGoodThings();
       //小王不酗酒,喜欢喝可乐……
       xw.drink();
       //自动调用toString方法
       System.out.println(xw);
       System.out.println("********************");
       //依旧先调用父类适合的构造器
       //创建家住在浙江王xx
       LittleXiaoWang lxw = new LittleXiaoWang("小小王", "浙江");
       System.out.println(lxw.getLastName() + "," + lxw.address);//小小王,浙江
       //小小王做好事……
       lxw.doGoodThings();
       //小小王不喜欢喝可乐,喜欢雪碧……
       lxw.drink();
   }
}
//测试
创建家住在浙江王xx
小王,浙江
小王做好事……
小王不酗酒,喜欢喝可乐……
Object类是所有类的直接父类或间接父类
********************
创建家住在浙江王xx
小小王,浙江
小小王做好事……
小小王不喜欢喝可乐,喜欢雪碧……

Inherited characteristics

Syntax

[修饰符] class Subclass extends Superclass
{
    //类定义部分
}

Parent-child relationship class

  • extends keyword is inherited, Subclass is a subclass , also called a derived class, Superclass is the parent class , also called the base class, superclass.
  • Once the subclass inherits the parent class, you will get all of the parent class structure , that is, properties and methods. But the parent class with private members modified (lastName), a subclass can not directly access, but accessible methods provided by the parent to prove, as xw.getLastName().
XiaoWang xw = new XiaoWang("小王", "浙江");
//false:System.out.println(l.lastName);
System.out.println(xw.getLastName() + "," + xw.address);//小王,浙江
  • Up to a subclass of a direct parent , but can have an unlimited number of indirect parent class; a parent can have many subclasses.
//下面的语句错误!
class XiaoWang extends LaoLi,LaoWang{...}
  • Subclasses parent class is a relative concept, a class may be a parent class may be sub-categories, such as the embodiment XiaoWang.
  • If there is no direct parent explicitly specify a class, then the class will default extends java.lang.Object class, that is to say the Object class is the class of all direct or indirect parent class . After the Object class will take on at some length to summarize.
//重写Object类的toString方法
public String toString(){
    return "Object类是所有类的直接父类或间接父类";
}

Inheritance points

Apart from a few mentioned below, there are many, many, to be supplemented!

Override the parent class method

Subclass the parent class can be based on additional new member variable or method. You can also override the parent class method , the same method name is added to the list of parameters, but to define different behaviors. (In the next rewrite about ......)

//父类的方法
public void drink() {
    System.out.println("老王酗酒……");
}
//重写父类的方法
public void drink() {
    System.out.println(this.getLastName() + "不酗酒,喜欢喝可乐……");
}

Inheritance constructor

Inheritance of constructors , next will be explained in detail, temporarily vacant.

//super类构造器
public LaoWang() {
    System.out.println("未知信息老王创建");
}

public LaoWang(String address) {
    this.address = address;
    System.out.println("创建家住在" + this.address + "王xx");
}

 //子类构造器
XiaoWang(String lastName, String address) {
    //super调用父类构造器
    super(address);
    setLastName(lastName);
}

The succession of super keyword

super keyword for defining the object instance variable or method call its parent class inheritance obtained. super can not happen again static statement . (After supplement ..)

 //super调用父类构造器
    LittleXiaoWang(String n, String a) {
        super(n, a);
    }

...

Note: This article has some content to be added that although the contents of the back are covered, think little by little eroded knowledge, so forgive me, certainly the first time to add!

Reference books: "crazy Java handouts"

Guess you like

Origin www.cnblogs.com/summerday152/p/12020096.html