010-Object-oriented inheritance

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to look at the corresponding picture below)

1. The introduction of inheritance

In order to describe and process personal information, define the class Person:
010-Object-oriented inheritance
010-Object-oriented inheritance
to describe and process student information, define the class Student:
010-Object-oriented inheritance
010-Object-oriented inheritance
Through observation, we found that the Student class and the Person class have some common attributes. In fact, according to our real life experience, We should also know that students should also be humans, so we can reduce our code writing through inheritance and simplify the definition of the
010-Object-oriented inheritance
010-Object-oriented inheritance
Student class : The Student class inherits all the properties and methods of the parent class Person, and adds an attribute school . The attributes and methods in Person can be used by Student. Also similar:
010-Object-oriented inheritance

2. Why should there be inheritance?

When the same attributes and behaviors exist in multiple classes, extract these contents into a single class, so multiple classes do not need to define these attributes and behaviors, as long as they inherit that class. The multiple classes here are called subclasses (derived classes), and this single class is called the parent class (base class or super class), which can be understood as: "subclass is a parent class".
Class inheritance syntax rules:
class SubClass extends SuperClass {
//...
}

Three, the role of inheritance

1. The emergence of inheritance reduces code redundancy and improves code reusability.
2. The emergence of inheritance is more conducive to the expansion of functions.
3. The emergence of inheritance creates a relationship between classes and provides a prerequisite for polymorphism.

Four, matters needing attention

1. Don't inherit just to get a certain function in other classes.
2. When the child class inherits the parent class, it inherits the methods and properties of the parent class.
3. In the subclass, you can use the methods and attributes defined in the parent class, or you can create new data and methods.
4. In Java, the inherited keyword is "extends", that is, the child class is not a subset of the parent class, but an "extension" to the parent class.

Five, inheritance rules

1. Subclasses cannot directly access private member variables and methods in the parent class.
010-Object-oriented inheritance
2. Java only supports single inheritance and multiple inheritance, and multiple inheritance is not allowed, that is: a subclass can only have one parent class, and a parent class can derive multiple subclasses.
010-Object-oriented inheritance
010-Object-oriented inheritance
010-Object-oriented inheritance

Six, single inheritance and multi-layer inheritance examples

010-Object-oriented inheritance

Seven, practice

1. Define a student class Student, which inherits from the Person class
010-Object-oriented inheritance
2. Define a ManKind class, including:
(1) Member variables int sex and int salary; method void manOrWoman(), according to the value of sex to display "man" (sex= =1) or "woman" (sex==0); method void employeed(), according to the value of salary, display "no job" (salary==0) or "job" (salary!=0).
(2) Define the class Kids to inherit ManKind and include: the member variable int yearsOld; the method printAge() prints the value of yearsOld.
(3) Define the class KidsTest, instantiate the Kids object someKid in the main method of the class, and use this object to access the member variables and methods of its parent class.

Guess you like

Origin blog.51cto.com/12859164/2547226