Object oriented day 4:
First day of submarine game:
- Six classes are designed, the World class is created and tested
Day 2 of submarine game:
- Add constructor to 6 classes and test
Day 3 of submarine game:
- Design and test reconnaissance submarine array, torpedo submarine array, mine submarine array, mine array and bomb array
- Design SeaObject superclass and 6 classes to inherit superclass
- Two construction methods are designed in the super class, and six classes are called respectively
Day 4 of submarine game:
- The reconnaissance submarine array, torpedo submarine array and mine submarine array are combined into SeaObject array and tested
- Override move() in 6 derived classes to move and test (unit test)
- Draw window: in the World class, step 3 ---------- not required to master
- imoprt JFrame+JPanel
- Designing the World class to inherit JPanel ----------------- this step is particularly easy to forget
- The eight sentences in main ------ CV Dafa
Review:
-
Reference type array:
-
Assigning a value to an element requires new objects
-
Access needs to be managed through array elements
Student[] stus = new Student[3]; stus[0] = new Student("zhangsan",25,"LF"); System.out.println(stus[0].name);
-
-
Succession:
Code reuse, inheritance through extensions, super class: common derived class: unique
The derived class can be accessed: the superclass of the + superclass of the derived class cannot access the superclass of the derived class
Single inheritance, transitivity
Specifies that a superclass must be constructed before a derived class is constructed
If the superclass construction is not called in the derived class construction, the superclass nonparametric construction will be called by default
In the derived class construction, if you call the super class construction, it will no longer be provided by default
-
super: refers to the superclass object of the current object
super. Member variable name ----------------------- access the member variable of the superclass
super. Method name () ---------------------------- call the method of superclass
super() ---------------------------------------- call the constructor of superclass
Notes:
-
Shape up:
- A reference to a supertype points to an object of a derived class
- What you can point out depends on the type of reference ----------- this is the rule. Just remember it
public class UploadDemo { public static void main(String[] args) { Aoo o1 = new Aoo(); o1.a = 1; o1.show(); //o1.b = 2; // Compilation error, superclass cannot access the of derived class //o2.test(); // Compilation error Boo o2 = new Boo(); o2.b = 1; o2.test(); o2.a = 2; //Correctly, derived classes can access the of superclasses o2.show(); //correct Aoo o3 = new Boo(); //Upward modeling o3.a = 1; o3.show(); //o3.b = 2; // Compilation errors. What can you point out and see the type of reference //o3.test(); // Compilation error } } class Aoo{ int a; void show(){ } } class Boo extends Aoo{ int b; void test(){ } }
-
Override / overriding of methods: Rewrite
- Occurs in the parent-child class, with the same method name and parameter list ----- the method of the superclass is overridden in the derived class
- When the rewriting method is called, it depends on the type of the object - this is the rule, just remember
class Restaurant{ void Cook a meal(){ Cook Chinese food } //Thinking + class over ------------ 3:10 continue } //1) I still want to make Chinese food ---------- no need to rewrite it class Aoo extends Restaurant{ } //2) I want to change to Western food ------------- need to rewrite class Aoo extends Restaurant{ void Cook a meal(){ Make western food } } //3) I want to add Western food to Chinese food ---- need to rewrite (super Chinese food first, then western food) class Aoo extends Restaurant{ void Cook a meal(){ super.Cook a meal(); + Make western food } }
-
As like as two peas, two and two, we should follow the same principle.
- Same as:
- Same method name
- The parameter list is the same
- Two hours:
- The return value type of the derived class method is less than or equal to that of the superclass method
- void and base type must be equal
- When referencing a type, it can be less than or equal to
- Say again when the exception thrown by the derived class method is less than or equal to the ----------- API of the superclass method
- The return value type of the derived class method is less than or equal to that of the superclass method
- Major one:
- The access permission of the derived class method is greater than or equal to that of the super class method ------------ tomorrow morning
//Super class is large and derived class is small class Coo{ void show(){} double test(){ return 0.0; } Student say(){ return null; } Person sayHi(){ return null; } } class Doo extends Coo{ //int show(){ return 1; } // Compilation error, void must be equal //int test(){ return 0; } // Compilation error, base type must be equal //Person say(){ return null; } // Compilation error, reference type must be less than or equal to Student sayHi(){ return null; } //Correct, Student is less than Person }
- Same as:
-
The difference between rewriting and overloading: ------------ common interview questions
- Override: occurs in parent and child classes, with the same method name and parameter list
- Overload: occurs in the same class, with the same method name and different parameter lists
Essence notes:
- Shape up:
- A reference to a supertype points to an object of a derived class
- What you can point out depends on the type of reference ----------- this is the rule. Just remember it
- Override / overriding of methods: Rewrite
- Occurs in the parent-child class, with the same method name and parameter list ----- the method of the superclass is overridden in the derived class
- When overriding a method to be called, look at the type of the object ------------------------------ this is the rule. Just remember
- As like as two peas, two and two, we should follow the same principle.
- Same for both:
- Same method name
- The parameter list is the same
- Two hours:
- The return value type of the derived class method is less than or equal to that of the superclass method
- void and base type must be equal
- When referencing a type, it can be less than or equal to
- Say again when the exception thrown by the derived class method is less than or equal to the ----------- API of the superclass method
- The return value type of the derived class method is less than or equal to that of the superclass method
- Major one:
- The access permission of the derived class method is greater than or equal to that of the super class method ------------ tomorrow morning
- Same for both:
- The difference between rewriting and overloading: ------------ common interview questions
- Override: occurs in parent and child classes, with the same method name and parameter list
- Overload: occurs in the same class, with the same method name and different parameter lists
Supplement:
-
Inheritance should conform to the relationship of is (yes)
-
When to shape up:
-
When multiple characters do almost the same thing, they can be unified into an array for reuse
eg: students / teachers / doctors are outputting names and saying hello, so they are unified into the Person array, a for
Then, realize the reuse of code
eg: all submarines can do almost the same thing, so the three submarines are unified into the SeaObject array
-
-
Tomorrow words:
1)status:state 2)package:package 3)import:Import 4)public:Open 5)protected:Protected 6)private:Private 7)card:card 8)id:number 9)password/pwd:password 10)balance:balance 11)pay:payment 12)money:amount of money 13)check:inspect 14)static:Static 15)image:picture 16)icon:Icon 17)get:obtain