[Detailed + super basic] Java-study notes 07

Two, abstract class

2.1 Overview of abstract classes (understanding)

When we are doing the extraction of common features of subclasses, some methods are not concretely reflected in the parent class. At this time, abstract classes are needed! In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be defined as an abstract class!

2.2 The characteristics of abstract classes (memory)

  • Abstract classes and abstract methods must be decorated with the abstract keyword
//抽象类的定义
public abstract class 类名 {
    
    }
//抽象方法的定义
public abstract void eat();
  • Abstract classes do not necessarily have abstract methods, classes with abstract methods must be abstract classes

  • Abstract class cannot be instantiated

​ How to instantiate an abstract class? Refer to the way of polymorphism and instantiate through subclass objects, which is called abstract polymorphism

  • Subclass of abstract class

​ Or rewrite all abstract methods in the abstract class

​ Either an abstract class

2.3 Member characteristics of abstract classes (memory)

Characteristics of members

  • Member variables

​ can be both variables

​ can also be a constant

  • Construction method

​ Empty parameter structure

​ Parameter structure

  • Member method

​ Abstract method

​ Ordinary method

  • Code demo

Animal

public abstract class Animal {
    
    
private int age = 20;
private final String city = "北京";
public Animal() {
    
    }
public Animal(int age) {
    
    
this.age = age;
}
public void show() {
    
    
age = 40;
System.out.println(age);
// city = "上海";
System.out.println(city);
}
public abstract void eat();
}

Cats

public class Cat extends Animal {
    
    
@Override
public void eat() {
    
    
System.out.println("猫吃鱼");
}
}

Test class

public class AnimalDemo {
    
    
public static void main(String[] args) {
    
    
Animal a = new Cat();
a.eat();
a.show();
}
}

2.4 Cases of abstract classes (applications)

  • Case requirements

Please use the idea of ​​abstract classes to implement the cat and dog cases, and test them in the test class

  • Code

Animal

public abstract class Animal {
    
    
private String name;
private int age;
public Animal() {
    
    
}
public Animal(String name, int age) {
    
    
this.name = name;
this.age = age;
}
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 abstract void eat();
}

Cats

public class Cat extends Animal {
    
    
public Cat() {
    
    
}
public Cat(String name, int age) {
    
    
super(name, age);
}
@Override
public void eat() {
    
    
System.out.println("猫吃鱼");
}
}

Dogs

public class Dog extends Animal {
    
    
public Dog() {
    
    
}
public Dog(String name, int age) {
    
    
super(name, age);
}
@Override
public void eat() {
    
    
System.out.println("狗吃骨头");
}
}

Test class

public class AnimalDemo {
    
    
public static void main(String[] args) {
    
    
//创建对象,按照多态的方式
Animal a = new Cat();
a.setName("加菲");
a.setAge(5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
System.out.println("­­­­­­­­");
a = new Cat("加菲",5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
}
}

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/113360581