Java classes must contain one or more constructors, the greatest benefit of using a constructor is to initialize the field of the class when the object is created (when creating a
When building an object, the system initializes the field for the object, if the base type is assigned a value of 0, if it is a reference-type
field is assigned null), such as the following code:
public class Teacher {private String name;private int age;public Teacher () {}public Teacher (String name,int age) {THIS.name =name;this.age=age;} @Overridepublic String toString () {return "name=" +name+ "\nage=" +age;}}
The above code provides a custom constructor that we initialize with this constructor.
Let's look at how to use a custom constructor to create an object and initialize it:
public class Main {public static void main (string[] args) {Teacher teacher=new Teacher ("Bill", 25); System.out.println (Teacher.tostring ());}}
Output Result:
Name=bill
Age=25
It is worth noting here that when we call the constructor, the system allocates memory space for the object and performs the default initialization
, that is, the system has created an object before the system starts executing the constructor, except that the object cannot be accessed externally, only in the construction
Referenced by this in the device.
As we mentioned earlier, when we provide our own constructors, the system no longer provides a default constructor, and if necessary, you can define the non-parametric constructs yourself
Manager
Before we know the same method names in the same class, the different parameter lists are called overloads of the methods. The same is true for constructors, where the shape of multiple constructors
The parameter list is different, which is called the constructor's overload (the name of the constructor must be consistent with the class name).
Class inheritance is one of the three main object-oriented features (previously described in encapsulation), inheritance is an important means of implementing software reuse, we learn that Java has a single relay
Only one direct parent class for each subclass. Inheritance can be implemented by the extends keyword, and the class that implements the inheritance is called a subclass,
The inherited class is called the parent class (base class, Super Class)
The following are the inherited syntax formats:
public class Mathteacher extends Teacher {}
How does inheritance work, let's look at the following code:
public class Teacher {public String name;public int age;public Teacher () {}public Teacher (String name,int age) {This.name=n Ame;this.age=age;} @Overridepublic String toString () {return "name=" +name+ "\nage=" +age;}}
for convenience, we changed access to public for the field of the teacher class, and for an explanation of the access control, see (Java Object-oriented Note 2)
Then write the main entry:
public class Main {public static void main (string[] args) {mathteacher teacher=new mathteacher (); teacher.name= "Bill"; teacher.age=25; System.out.println (Teacher.tostring ());}}
Output Result:
Name=bill
Age=25
In the above we define an empty Mathteacher class, and inherit the class teacher, in the main method, create the Mathteacher object, and
Access and assignment of this object's name and age, as can be seen here, the Mathteacher object also has the teacher instance field and the ToString method.
Subclasses extend the parent class, which is a special parent class, and usually the subclass is always based on the parent class, adding new field and methods. But there is a
Exceptions, such as birds have a way of flying, where ostriches are birds, but it flies in a different way than other birds, so we need to rewrite the bird's
Flight methods to meet the ostrich's flight patterns, the code is as follows:
Define a bird's class
public class Bird {public void Fly () {System.out.println ("Flying in the Sky");}}
and then define an ostrich class.
public class Ostrich extends Bird {@Overridepublic void Fly () {System.out.println ("Run on the Ground")}}
Finally create the Ostrich object
public class Main {public static void main (string[] args) {Ostrich ostrich=new ostrich (); Ostrich.fly ();}}
Output Result:
Run on the ground
From the above we can see that the method of fly is no longer the fly method of bird class, but the fly method of ostrich class is executed. This seed class contains the
The behavior of a method with the same name as the parent class is called a method override, also known as overwrite. Method of
Rewrite to follow "two with two small one big", "two identical" is the value method name is the same, the formal parameter list is the same; "Two small" refers to the subclass method return value type than the parent class method returned
The return value type is smaller or equal, and the exception that the subclass method declaration throws should be smaller or equal than the exception that is thrown by the parent method declaration; "A large" refers to the class of the subclass method.
Access permissions should be greater or equal than the access rights of the parent class method.
In subclasses we can invoke the instance method of the parent class overridden by Super, which is a keyword provided by Java to qualify the object to call it
The field or method inherited from the parent class, super cannot appear in the static decorated method.
The initialization code of the parent class constructor can be called by super in the constructor of the subclass, as in the following code:
public class Bird {private int weight;public Bird (int weight) {this.weight=weight;} public void Fly () {System.out.println ("Flying in the Sky");}}
public class Ostrich extends Bird {public ostrich (int weight) {super (weight);} @Overridepublic void Fly () {System.out.println ("running on the Ground");}}
public class Main {public static void main (string[] args) {Ostrich ostrich=new ostrich (); Ostrich.fly ();}}
In the above program, the constructor of the parent class is called by Super and initialized (it is worth noting that this and super do not appear at the same time). Its
The subclass constructor always calls the parent class constructor once, regardless of whether we use the super call to execute the initialization code of the parent class constructor. That is when
When a subclass constructor is called to initialize a child class object, the parent class constructor is always executed before the subclass constructor.
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43866163 Emotional Control _
Java Object-oriented Note 3