java self road six (inherited)

inherit

format:

  • public class name of the subclass extends the parent class name} {
public class Employee {
    public void method() {
        System.out.println("方法执行!");
    }
}
public class Teacher extends Employee{
    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        teacher.method();
    }
}

super

Access the parent class of the same name variables

    public void method(){
        int age = 20;
        System.out.println(age);  //20
        System.out.println(this.age);  //25
        System.out.println(super.age);  //30
    }

Rewrite

@Override can detect whether a rewrite success

note:

  • The return value is a subclass of method, the parent must be less than equal to the return value range class. java.lang.object is the highest common father of all classes

  • Permissions subclass method must be greater than the parent class method permission modifier.

    public> protected> (default)> private, default is not a keyword, but nothing to write, leave it blank.

Design Principles

For the class already in use, try not to be modified, it is recommended to define a new class, to which the reuse of common content and add new content changes

Features access parent-child class constructor

note:

  1. Subclass constructor which has a default implicit super () call, so be sure is to call the parent, after calling the subclass.
  2. You can be constructed by super keyword to call the superclass constructor overloading
  3. Parent class constructor calls super must be the first statement of the subclass constructor, not a sub-class constructor called multiple times super structure
public class Father {
    public Father(){
        System.out.println("父类构造方法");
    }
}
public class Son extends Father {
    public Son(){
        System.out.println("子类构造方法");
    }

    public static void main(String[] args) {
        Son son = new Son();
    }
}

Where arguments, calling the parent class constructor overloaded with super

    public Father(int age){
        System.out.println(age);
    }
    public Son(){
        super(13);
        System.out.println("子类构造方法");
    }

    public static void main(String[] args) {
        Son son = new Son();
    }

Summary: The subclass must call the parent class constructor, do not write the gift super (), written by the writing of specified super call must be the first line.

this usage

public class Daughter {
    public Daughter(){
        this(23);  // 本类的无参构造,调用本类的有参构造。
    }
    public Daughter(int n){
        
    }
}
  1. In the class member method, access the member variables of this class.
  2. In the method of the present class member, another member of this class access method.
  3. In the present method, the constructor, another configuration access to the class method. (This (...) call must be the first statement in the constructor, the only one.)

Inheritance feature (important)

java language is the single inheritance

Abstract method

Plus the abstract keyword, remove the braces, direct semicolon

Abstract class where the method must be abstract class, before class write the abstract to

public abstract class Animal {
//    抽象方法
    public abstract void eat();
    public void method(){
        
    }
}

How to use abstract methods and classes

  1. Abstract class can not directly create new objects.

  2. Must inherit the abstract parent class with a subclass.

  3. Subclasses must override the parent override all abstract methods which remove the abstract keyword abstract method, then the method is not body braces.

  4. Create a sub-class objects use

    public abstract class Animal {
    //    抽象方法
        public abstract void eat();
        public void method(){
            System.out.println("执行了animal方法");
        }
    }
    
    public class Dog extends Animal{
        public void eat(){
            System.out.println("小狗吃东西");
        }
    }
    public class Demo01 {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.eat();
        }
    }
    

If the inherited class does not override all the abstract method, or an abstract class.

Exercises bonus package

to sum up:

  1. Paradigm capitalized

  2. Subclass Gets the parent class properties with get set

    public class User {
        private String name;
        private double money;
    
        public User() {
        }
    
        public User(String name, double money) {
            this.name = name;
            this.money = money;
        }
    
        public void show(){
            System.out.println("我是"+name+",有"+getMoney()+"元");
        }
    
        public String getName() {
            return name;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    }
    
    public class Host extends User{
        public Host() {
        }
    
        public Host(String name, double money) {
            super(name, money);
        }
    
        public ArrayList<Double> send(double gift, int count){
            ArrayList<Double> moneyArray = new ArrayList<>();
            double money = super.getMoney() - gift;
            if(gift > getMoney()){
                System.out.println(getMoney());
                System.out.println("余额不足");
                return moneyArray;
            }
            for (int i = 0; i < count; i++) {
                if(i==count-1){
                    double lastGift = gift;
                    moneyArray.add(lastGift);
                }else{
                    double perGift = new Random().nextDouble()*(Math.floor(gift));
                    gift -= perGift;
                    moneyArray.add(perGift);
                }
            }
    
    
    
            setMoney(money);
            return moneyArray;
        }
    }
    
    public class Customer extends User{
        public Customer() {
        }
    
        public Customer(String name, double money) {
            super(name, money);
        }
    
    
        public double receive(ArrayList<Double> moneyArray){
            int index = new Random().nextInt(moneyArray.size());
            double delta = moneyArray.get(index);
            moneyArray.remove(index);
            super.setMoney(super.getMoney() + delta);
            return delta;
        }
    }
    public class Demo02Test {
        public static void main(String[] args) {
            Host host = new Host();
            Customer customer1 = new Customer();
            Customer customer2 = new Customer();
            Customer customer3 = new Customer();
            Customer customer4 = new Customer();
            host.setName("群主");
            customer1.setName("二哈");
            customer2.setName("三哈");
            customer3.setName("四哈");
            customer4.setName("五哈");
            host.setMoney(29);
            customer1.setMoney(0);
            customer2.setMoney(0);
            customer3.setMoney(0);
            customer4.setMoney(0);
    
    
            ArrayList<Double> res = host.send(27, 4);
            host.show();
            double r1 = customer1.receive(res);
            customer1.show();
            double r2 = customer2.receive(res);
            customer2.show();
            double r3 = customer3.receive(res);
            customer3.show();
            double r4 = customer4.receive(res);
            customer4.show();
            System.out.println(r1 + r2 + r3 + r4);
        }
    }
    

Guess you like

Origin www.cnblogs.com/jimmyhe/p/11566910.html