The tenth day of Java basic learning (inheritance, abstract classes, value exchange)

1. Inheritance

1. Common relationships between classes (the relationship between the whole and the part)

//球员类
class Player{   
    int num; //编码
    String name;
    public Player(int num , String name){
        this.num = num;
        this.name = name;
    }
    public void run(){
        System.out.println(name+"开跑...");
    }
}
//球队类
class Team{
    String name;  //球队的名字
    Player p1;  //球员1 
    Player p2;  //球员2
    Player p3;  //球员3
    public  Team(String name,Player p1,Player p2,Player p3){
        this.name = name;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }   
    //开始比赛
    public void startGame(){
        System.out.println(name+"开赛啦!!");
    }
}
class Demo10.1{
    public static void main(String[] args){
        Player p1 = new Player(12,"梅西");
        Player p2 = new Player(7,"C罗");
        Player p3 = new Player(11,"内马尔");
        //球队
        Team t = new Team("恒大",p1,p2,p3);
        t.startGame();      
        System.out.println("名字:"+ t.p2.name);
    }
}

2. Inheritance: Inheritance is reflected by the keyword extends.

3. Inherited format

class 类名1 extends 类名2{

    }

4. Matters needing attention in inheritance:
① Do not inherit in order to reduce repetitive code, only inherit when there is a real inheritance relationship
② The private members of the parent class cannot be inherited.
③ The constructor of the parent class cannot be inherited.
④ When creating a subclass object, the constructor without parameters of the parent class will be called by default.

//人类 
class Person{
    String name;
    private int age;
    public  Person(String name){
        this.name = name;
    }
    public Person(){
        System.out.println("Person类的构造方法被调用了....");
    }
    public void eat(){
        System.out.println(name+"在吃饭...");
    }
}
//学生类
class Student extends Person {  // Student称作为Person类的子类,Person类就称作为Student的父类(超类、基类)
    int num; //学号
    public Student(){
        System.out.println("Student类的构造方法被调用了....");
    }
    public void study(){
        System.out.println(name+"好好学习!");
    }   
}
class Demo10.2{
    public static void main(String[] args){
        Student s = new Student();      
        s.name = "狗娃";
        System.out.println("名字:"+ s.name);
        s.eat();
    }
}

5. Calling the constructor of the parent class can initialize the properties inherited from the parent class.

class Fu{   
    int x = 10;
    String name;
    public Fu(String name){
        this.name = name;
        System.out.println("Fu类d带参的构造方法...");
    }
    public Fu(){
        System.out.println("Fu类无参的构造方法...");
    }
}
class Zi extends Fu{
    int x = 20;
    public Zi(String name){
        super(name); //指定调用父类一个参数的构造函数。
    }
    public void print(){
        System.out.println("x1 = "+ x);
    }

}
class Demo10.3{
    public static void main(String[] args){
        Zi z = new Zi("大头儿子"); 
        System.out.println("name= "+z.name);
    }
}

6, super keyword
① The super keyword represents the reference of the parent class space.
② The function of super keyword
◆ When there is a member with the same name in the child and parent class, the default is to access the members of the subclass in the subclass, and the members of the parent class can be specified through the super keyword.
◆ When a subclass object is created, the constructor without parameters of the parent class will be called first by default, and the constructor of the parent class can be called by specifying the super keyword.
③ Points to note when calling the superclass constructor with the super keyword
◆ If the subclass constructor does not specify to call the superclass constructor, the java compiler will add a super() statement to the subclass constructor.
◆ When the super keyword calls the constructor of the superclass, the statement must be the first statement in the constructor of the subclass.
◆ super and this keywords cannot appear in the same constructor to call other constructors at the same time. Because both statements require the first statement.
④ The difference between super keyword and this keyword
◆ The things represented are inconsistent.
The super keyword represents a reference to the parent class space. The this keyword represents the caller object of the owning function.
◆ The premise of use is inconsistent.
The super keyword must have an inheritance relationship before it can be used. The this keyword can be used without an inheritance relationship.
◆ The difference between calling the constructor The
super keyword is calling the constructor of the parent class. The this keyword is to call the constructor of this class.

7. Method rewriting
① The premise of method rewriting: There must be an inheritance relationship.
② Method rewriting: The child and parent class has a function with the same name, which we call method rewriting.
③ When to use method rewriting: When the function of the parent class cannot meet the needs of the subclass.
④ Matters needing attention in
method rewriting: ◆ When rewriting a method, the method name and the formal parameter list must be consistent.
◆ When the method is overridden, the permission modifier of the subclass must be greater than or equal to the permission modifier of the parent class.
◆ When the method is overridden, the return value type of the subclass must be less than or equal to the return value type of the parent class.
◆ When the method is overridden, the exception type thrown by the subclass should be less than or equal to the exception type thrown by the superclass.
Exception (worst), RuntimeException (little bad)
⑤ Requirements for method overloading
◆ Function names should be consistent.
◆ The formal parameter list is inconsistent (the number of formal parameters or the types of formal parameters are inconsistent)
◆ It has nothing to do with the return value type.

class Animal{}  //大的数据类型 
class Fish extends Animal{}  //小的数据类型
class Fu{
    String name;
    public Fu(String name){
        this.name = name;
    }
    public Animal eat() throws RuntimeException {
        System.out.println(name+"吃番薯...");
        return new Animal();
    }
}
class Zi extends Fu{
    String num; 
    public Zi(String name){
        super(name);//指定调用父类带参的构造方法
    }
    //重写父类的eat方法
    public Animal eat() throws Exception{
        System.out.println("吃点开胃菜..");
        System.out.println("喝点汤....");
        System.out.println("吃点龙虾....");
        System.out.println("吃青菜....");
        System.out.println("喝两杯....");
        System.out.println("吃点甜品...."); 
        return new Animal();
    }
}
class Demo10.4{
    public static void main(String[] args){
        Zi z = new Zi("大头儿子");
        z.eat();    
    }
}

8. The instanceof keyword
① Function: Determine whether an object belongs to a specified category.
② Prerequisites for use: There must be an inheritance or implementation relationship between the judged object and the specified category.
③ Use format: Object instanceof category
④ Function: Polymorphism. Generally, we will use this keyword to judge before doing forced type conversion, and then perform the conversion.

class Animal{
    String name;
    String color;
    public Animal(String name, String color){
        this.name = name;
        this.color = color;
    }
}
//狗是属于动物中一种
class Dog extends Animal {
    public Dog(String name,String color){
        super(name,color); //指定调用父类两个 参数的构造函数。
    }
    public void bite(){
        System.out.println(name+"咬人!!");
    }
}
//老鼠也是属于动物中一种
class Mouse extends Animal{ 
    public Mouse(String name,String color){
        super(name,color);
    }   
    public void dig(){
        System.out.println(name+"打洞..");
    }
}
class Demo10.5{
    public static void main(String[] args){
        Dog d = new Dog("哈士奇","白色");
        System.out.println("狗是狗类吗?"+(d instanceof Dog));//true
        System.out.println("狗是动物类吗?"+(d instanceof Animal));//true
        System.out.println("狗是老鼠类吗?"+ (d instanceof Mouse));=//报错
        Animal a = new Animal("狗娃","黄色"); 
        System.out.println("动物都是狗吗?"+ (a instanceof Dog));//false
    }
}

9.
The usage of the final keyword:
① When the final keyword modifies a variable of a basic type, the variable cannot be reassigned, and the first value is final.
② When the fianl keyword modifies a reference type variable, the variable cannot point to a new object again.
③ When the final keyword modifies a function, the function cannot be rewritten.
④ When the final keyword modifies a class, the class cannot be inherited.

The modifiers of constants are generally: public static final

//圆形
class Circle{
    double r; //半径
    public static final double PI = 3.14; //固定不变的
    public Circle(double r){
        this.r = r;
    }
    //计算面积
    public final void getArea(){
        System.out.println("圆形的面积是:"+r*r*PI);
    }
}
class Demo10.6 extends Circle{  
    public Demo10.6(double r){
        super(r);
    }
    public static void main(String[] args) 
    {
        final Circle c = new Circle(4.0);
        test(c);

        Demo10.6 c = new Demo10.6(4.0);
        c.getArea();
    }   
    public static void test(Circle c){
        c = new Circle(5.0);  //c变量又重新指向了新的对象。
        c.getArea();
    }
}

The abstract class

1. Application scenarios of abstract classes When
we describe a class of things, we find that this kind of thing does have a certain behavior, but this behavior is not specific at present, then we can extract the declaration of this behavior, but do not go to To achieve this behavior, at this time we call this behavior abstract behavior, we need to use abstract classes.

2. The benefits of abstract classes: It is mandatory that subclasses must implement the specified methods.

3. Details to be paid attention to in abstract classes:
① If a function does not have a method body, then the function must be modified with abstract, and the function must be modified into an abstract function.
② If an abstract function appears in a class, the class must also be modified with abstract.
③ If a non-abstract class inherits an abstract class, then all abstract methods of the abstract class must be implemented.
④ An abstract class can have non-abstract methods or abstract methods.
⑤ An abstract class can have no abstract methods.
⑥ abstract classes cannot create objects.
Question: Why can't abstract classes create objects?
Answer: Because abstract classes have abstract methods, if the abstract class can create objects, it makes no sense to use abstract objects to call abstract methods.
⑦ The abstract class has a constructor, and its constructor is provided to the subclass to initialize the properties of the superclass when creating an object.

//动物类--->抽象类
abstract class Animal{  
    String name;
    String color;
    public Animal(String  name,String color){
        this.name = name;
        this.color = color;
    }
    //非抽象的方法
    public void eat(){
        System.out.println(name+"吃粮食");
    }
    //移动...
    public abstract void run();
}
//狗,是属于动物中一种 
class Dog extends Animal{
    public Dog(String name,String color){
        super(name,color);
    }
    public void run(){
        System.out.println(name+"四条腿跑得很快...");
    }
}
//鱼,是属于动物中一种
class Fish extends Animal{
    public Fish(String name,String color){
        super(name,color);
    }
    public void run(){
        System.out.println(name+"摇摇尾巴游啊游!");
    }
}

4. Abstract cannot modify a method with the following keywords
① abstract cannot modify a method with private
② abstract cannot modify a method with static
③ abstract cannot modify a method with final

3. Value exchange

1. Passing by value: When calling a method, the parameters passed to the method actually pass the value stored in the variable.

import java.util.*;
class Person{
    int x =10;
}
class Demo10.7{
    public static void main(String[] args){
        int a = 3;
        int b = 5; 
        changeValue(a,b);
        System.out.println("交换之后的值:a = "+a +" b="+b); //值交换失败

        int[] arr = {23,10,9};
        changeArr(arr,1,2);
        System.out.println("数的元素:"+ Arrays.toString(arr));//值交换成功

        Person p = new Person();
        changeObj(p,20);
        System.out.println("x = "+ p.x);//20
    }   
    public static void changeObj(Person p ,int x){
        p.x = x;
    }   
    //需求2: 定义一个函数交换数组中两个元素的位置。
    public  static void changeArr(int[] arr , int index1,  int  index2){
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp; 
    }
    //需求1:定义一个函数交换两个基本类型变量的值。 
    public static void changeValue(int a , int b){
        int temp = a;
        a = b;
        b = temp;
        System.out.println("方法内部交换的值:a = "+a +" b="+b);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325946891&siteId=291194637