The constructor (constructor) is a special method. It is used primarily to initialize an object when it is created, to assign an initial value to an object member variable, and to always use the new operator in the statement that creates the object. A particular class can have more than one constructor, which is an overload of the constructor, depending on the number of arguments or different types of arguments. The function of a constructor is primarily used to define the state of the initialization when the class's object is created.
Constructs an object that first calls its constructor method to initialize its member functions and member variables.
A subclass has member variables and member methods of the parent, and if not called, the member variables and member methods inherited from the parent class are not properly initialized.
This can not be called in turn, because the parent class does not know that the child is a divine demon variable and that the subclass is not initialized with the parent class variable, causing the program to run an error!
1. Source code:
public class Explorationjdksource {
public static void Main (string[] args) {
System.out.println (New A ());
}
}
Class a{}
2. Results:
3. Results Analysis:
In the preceding example, the main method actually calls the public void println (Object x), which internally invokes the ValueOf method of the String class. The ValueOf method internally calls the Object.ToString method: Public String toString ()
{return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());}
The Hashcode method is a local method, implemented by the JVM Designer: public native int hashcode ();
3. Which of the following statements will cause a compilation error? Which one will cause a run-time error? Why?
M=d;
D=m;
D= (Dog) m;
D=c;
C= (Cat) m;
First make a self-judgment, draw a conclusion, run the Testcast.java instance code to see if your judgment is correct
Compile error
D=m;d=c;
An incorrect subclass object can be directly assigned to a base class variable.
To assign a base class object to a subclass object variable, you must perform a type conversion,
Its syntax is: Sub-class object variable = (subclass name) base class object name;
Run error c= (Cat) m
Incorrect conversion confusion. If the type conversion fails, Java throws the following exception: ClassCastException
4. What is the result of the program running below? 2. How do you explain that you will get such output? 3. The computer is not wrong, the reason why you get such a running result is also a reason, then from these running results, you can summarize the Java syntax features?
public class Parentchildtest {
public static void Main (string[] args) {
Parent parent=new parent ();
Parent.printvalue ();
Child child=new Child ();
Child.printvalue ();
Parent=child;
Parent.printvalue ();
parent.myvalue++;
Parent.printvalue ();
((child) parent). myvalue++;
Parent.printvalue ();
}
}
Class parent{
public int myvalue=100;
public void Printvalue () {
System.out.println ("Parent.printvalue (), myvalue=" +myvalue);
}
}
Class Child extends parent{
public int myvalue=200;
public void Printvalue () {
System.out.println ("Child.printvalue (), myvalue=" +myvalue);
}
}
1)
Parent.printvalue (), myvalue=100
Child.printvalue (), myvalue=200
Child.printvalue (), myvalue=200
Child.printvalue (), myvalue=200
3)
When a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type. If the child class has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword.
If the subclass is used as a parent class, the field accessed through the subclass is the parent class!
5. Why do I have to call the constructor method of the parent class before the constructor of the subclass is run? Can you turn around? Why can't it be reversed?
Cause: The constructor is used to initialize the object when the object is created, and with the new operator when creating the statement for the object. The subclass has member variables and member methods of the parent class, and if not called, the member variables and member methods inherited from the parent class are not properly initialized. Can not be called in turn, the parent class does not know what the child class has a variable, resulting in the child class is not properly initialized, the program error.
6. Polymorphic Meanings and uses
Let's look at a development scenario:
A zoo has a keeper, Xiao Li,
The Lions, monkeys, and pigeons He is responsible for feeding every day need to be fed.
Please use a program to simulate the process of his feeding.
① Three species of animals correspond to three classes, each defining a eat () method, which represents the food that the breeder gives them.
Redesign a feeder class to represent the breeder, whose Name field holds the breeder's name, three methods representing three different animals, each of which refers to three animal objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Zoo
{
public static void Main (String args[])
{
Feeder F = new Feeder ("Xiao Li");
Breeder Xiao Li feeds a lion
F.feedlion (New Lion ());
Breeder Xiao Li feeds 10 monkeys
for (int i = 0; i <; i++)
{
F.feedmonkey (New Monkey ());
}
Breeder Xiao Li feeds 5 pigeons
for (int i = 0; i < 5; i++)
{
F.feedpigeon (New Pigeon ());
}
}
}
Class Feeder
{
public String name;
Public feeder (String name)
{
THIS.name = name;
}
public void Feedlion (Lion l)
{
L.eat ();
}
public void Feedpigeon (Pigeon p)
{
P.eat ();
}
public void Feedmonkey (Monkey m)
{
M.eat ();
}
}
Class Lion
{
public void Eat ()
{
System.out.println ("I don't eat meat who dares to eat meat!") ");
}
}
Class Monkey
{
public void Eat ()
{
SYSTEM.OUT.PRINTLN ("I eat everything, especially bananas.") ");
}
}
Class Pigeon
{
public void Eat ()
{
SYSTEM.OUT.PRINTLN ("I want to lose weight, so eat only a little rice every day." ");
}
}
What is wrong with this kind of programming?
The class is created once per feed. Repeat steps more.
① Introduction of inheritance
Defines an abstract base class animal, which defines an abstract method eat (), three subclasses implement this abstract method.
The three feeding methods of the feeder class can now be combined into a feedanimal () method, noting that it receives a type of animal parameter instead of three specific animal types.
Depending on the polymorphic nature, this method will be able to receive any subclass object derived from the animal class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Zoo
{
public static void Main (String args[])
{
Feeder F = new Feeder ("Xiao Li");
Breeder Xiao Li feeds a lion
F.feedanimal (New Lion ());
Breeder Xiao Li feeds 10 monkeys
for (int i = 0; i <; i++)
{
F.feedanimal (New Monkey ());
}
Breeder Xiao Li feeds 5 pigeons
for (int i = 0; i < 5; i++)
{
F.feedanimal (New Pigeon ());
}
}
}
Class Feeder
{
public String name;
Feeder (String name)
{
THIS.name = name;
}
public void Feedanimal (Animal an)
{
An.eat ();
}
}
Abstract class Animal
{
public abstract void Eat ();
}
Class Lion extends Animal
{
public void Eat ()
{
System.out.println ("I don't eat meat who dares to eat meat!") ");
}
}
Class Monkey extends Animal
{
public void Eat ()
{
SYSTEM.OUT.PRINTLN ("I eat everything, especially bananas.") ");
}
}
Class Pigeon extends Animal
{
public void Eat ()
{
SYSTEM.OUT.PRINTLN ("I want to lose weight, so eat only a little rice every day." ");
}
}
① further optimize feeding of a group of animals
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Package Zoo3;
public class Zoo
public static void Main (String args[]) {
Feeder F = new Feeder ("Xiao Li");
animal[] ans = new animal[16];
Breeder Xiao Li feeds a lion
Ans[0] = new Lion ();
Breeder Xiao Li feeds 10 monkeys
for (int i = 0; i < i++) {
Ans[1 + i] = new Monkey ();
}
Breeder Xiao Li feeds 5 pigeons
for (int i = 0; i < 5; i++) {
Ans[11 + i] = new Pigeon ();
}
F.feedanimals (ANS);
}
}
Class Feeder {
public String name;
Feeder (String name) {
THIS.name = name;
}
public void Feedanimals (animal[] ans) {
for (Animal An:ans) {
An.eat ();
}
}
}
Abstract class Animal {
public abstract void Eat ();
}
Class Lion extends Animal {
public void Eat () {
System.out.println ("I don't eat meat who dares to eat meat!") ");
}
}
Class Monkey extends Animal {
public void Eat () {
SYSTEM.OUT.PRINTLN ("I eat everything, especially bananas.") ");
}
}
Class Pigeon extends Animal {
public void Eat () {
SYSTEM.OUT.PRINTLN ("I want to lose weight, so eat only a little rice every day." ");
}
}
④ after the second refactoring, the feeder class's Feedanimals () method receives a animal array, which has a limitation that only a fixed number of arrays can be created, and the number of animals cannot be dynamically increased or decreased.
Think about the following scenarios:
(1) New animals in the zoo
(2) An animal is ill and died of misfortune
(3) ...
Can our code cope with the above scenario?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Import Java.util.Vector;
public class Zoo {
public static void Main (String args[]) {
Feeder F = new Feeder ("Xiao Li");
vector<animal> ans = new vector<animal> ();
Breeder Xiao Li feeds a lion
Ans.add (New Lion ());
Breeder Xiao Li feeds 10 monkeys
for (int i = 0; i < i++) {
Ans.add (New Monkey ());
}
Breeder Xiao Li feeds 5 pigeons
for (int i = 0; i < 5; i++) {
Ans.add (New Pigeon ());
}
F.feedanimals (ANS);
}
}
Class Feeder {
public String name;
Feeder (String name) {
THIS.name = name;
}
Vector<t> is a collection of objects provided in the JDK that can be added to or removed from the object at any time
public void Feedanimals (vector<animal> ans) {
for (Animal An:ans) {
An.eat ();
}
}
}
Abstract class Animal {
public abstract void Eat ();
}
Class Lion extends Animal {
public void Eat () {
System.out.println ("I don't eat meat who dares to eat meat!") ");
}
}
Class Monkey extends Animal {
public void Eat () {
SYSTEM.OUT.PRINTLN ("I eat everything, especially bananas.") ");
}
}
Class Pigeon extends Animal {
public void Eat () {
SYSTEM.OUT.PRINTLN ("I want to lose weight, so eat only a little rice every day." ");
}
}
Summarize:
There are two main forms of polymorphic programming:
(1) Inheritance polymorphism: The method used by the sample program
(2) interface polymorphism: Use interface instead of abstract base class.
The greatest benefits of using polymorphism are:
When you want to modify the program and expand the system, you need to modify less places, the other part of the code has less impact! Don't underestimate these two "more" words! The larger the program size, the more prominent its advantages.
Java sixth chapter hands the brain