-
Override / overriding: rewrite and overwrite
- Occurs in parent-child classes, with the same method name and the same parameter list
- When overriding a method is called, look at the type of the object ------------- this is the rule. Remember that it's OK
When a derived class feels that the behavior of a superclass is not good enough, it can override
I inherited a Chinese restaurant class Aoo{ void do(){ Cook Chinese food } } A:I still want to cook Chinese food------------No need to rewrite class Boo extends Aoo{ } B:I want to make western food instead--------------Need to rewrite class Boo extends Aoo{ void do(){ Make western food } } C:I'd like to add Western food to Chinese food-----Need to rewrite(before super Chinese food, then western food) class Boo extends Aoo{ void do(){ super.do(); Make western food } }
-
The difference between rewriting and overloading: key points (common interview questions)
-
Override: occurs in parent and child classes, with the same method name and parameter list
Methods commonly used to modify superclasses in derived classes
-
Overload: occurs in the same class, with the same method name and different parameter lists
Is a completely different method, but the method name is the same
-
-
package and import:
-
Package: declaration package
- Function: avoid class naming conflicts
- Classes in the same package cannot have the same name. Classes in different packages can have the same name
- Full name of class: package name Class names and package names often have a hierarchy
- Suggestion: all letters of the package name are lowercase
Description: package declares that the package must be on the first line
-
Import: import class
-
Classes in the same package can be accessed directly, but classes in different packages cannot be accessed directly. If you want to access:
- import the class first, and then access the class ------------ suggestions
- The full name of the class --------------------------------------- too cumbersome, not recommended
Note: the import class must be on the next line of the declaration package
-
-
-
Access control modifier:
The meaning of encapsulation: hide something and expose something to protect the security of data
- Public: public, any class
- Private: private, this class
- Protected: protected, including this class, derived class and the same package class
- Default: write nothing, this class and the same package class
- Class can only be public or default
- The access permissions of the members in the class can be as follows
//Meaning of encapsulation class Card{ private String cardId; private String cardPwd; private double balance; public boolean payMoney(double money){ //Payment amount if(balance>=money){ balance-=money; return true; }else{ return false; } } public boolean checkPwd(String pwd){ //Detect password if(pwd And cardPwd identical){ return true; }else{ return false; } } } //Scope of access: package ooday04; //Demonstrate access control modifiers public class Aoo { public int a; //Any class protected int b; //This class, derived class, same package class int c; //This class and the same package private int d; //This category void show(){ a = 1; b = 2; c = 3; d = 4; } } class Boo{ //---------------Demo private void show(){ Aoo o = new Aoo(); o.a = 1; o.b = 2; o.c = 3; //o.d = 4; // Compilation error } } package ooday04_vis; import ooday04.Aoo; public class Coo { //Demonstrate the concept of the same package void show(){ Aoo o = new Aoo(); o.a = 1; //o.b = 2; // Compilation error //o.c = 3; // Compilation error //o.d = 4; // Compilation error } } class Doo extends Aoo{ //Demo protected void show(){ a = 1; b = 2; //c = 3; // Compilation error //d = 4; // Compilation error } }
-
Static: static
-
Static variables:
- Modified by static
- It belongs to a class and is stored in the method area. There is only one copy
- Often accessed through class name points
- When to use: data shared by all objects (pictures, audio, video, etc.)
public class StaticDemo { public static void main(String[] args) { Eoo o1 = new Eoo(); o1.show(); Eoo o2 = new Eoo(); o2.show(); Eoo o3 = new Eoo(); o3.show(); System.out.println(Eoo.b); //Often accessed through class name points } } class Eoo{ //Demonstrate static variables int a; static int b; Eoo(){ a++; b++; } void show(){ System.out.println("a="+a+",b="+b); } }
-
Static method:
- Modified by static
- It belongs to a class and is stored in the method area. There is only one copy
- Often accessed through class name points
- Static methods do not implicitly pass this, so they cannot directly access instance members
- When to use: the operation of the method is independent of the object
//static demo public class StaticDemo { public static void main(String[] args) { Goo.plus(4,6); } } //Demonstrate static methods class Foo{ int a; //Instance variables (accessed by objects) static int b; //Static variable (accessed by class name) void show(){ //Implicit this System.out.println(this.a); System.out.println(Foo.b); } static void test(){ //There is no implicit this pass in static methods //No this means no object //The instance variable a must be accessed by an object //Therefore, the following statement has a compilation error //System.out.println(a); // Compilation error System.out.println(Eoo.b); } } //Demonstrate when static methods are used class Goo{ int a; //Object properties //The attribute a of the object is used in the method, which means that the operation of show() is related to the object and cannot be made into a static method void show(){ System.out.println(a); } //The attribute and behavior of the object are not used in the method, which means that the operation of plus() is independent of the object and can be made into a static method static void plus(int num1,int num2){ int num = num1+num2; System.out.println(num); } }
-
Static block:
- Modified by static
- It belongs to a class and is automatically executed during class loading. A class is loaded only once, so the static block is also executed only once
- When to use: initialize / load static resources (pictures, audio, video, etc.)
public class StaticDemo { public static void main(String[] args) { Hoo o4 = new Hoo(); Hoo o5 = new Hoo(); Hoo o6 = new Hoo(); } } //Demonstrate static blocks class Hoo{ static { System.out.println("Static block"); } Hoo(){ System.out.println("Construction method"); } }
-
java object oriented fourth day
Posted by leeue on Sun, 16 Jan 2022 05:34:02 +0100