Purpose and requirements
- Master Java Modifiers
- Master the four major storage domains of Java
- Mastering Java Anonymous Inner Classes
- Java interface-oriented
Java Modifier
The Java language provides two types of modifiers:
- access modifier
- non-access modifier
1. Access modifier
modifier | current class | in the same package | descendant class (same package) | Descendant classes (different packages) | Other packages |
---|---|---|---|---|---|
public | Y | Y | Y | Y | Y |
protected | Y | Y | Y | Y/N (description) | N |
Default | Y | Y | Y | N | N |
private | Y | N | N | N | N |
2. Non-access modifier
static static:
1. Static variables
? The static keyword is used to declare static variables independent of objects. No matter how many objects a class instantiates, there is only one copy of its static variables. Static variables are also known as class variables. Local variables cannot be declared as static variables.
2. Static method
? The static keyword is used to declare a static method independent of the object. Static methods cannot use non-static variables of the class. Static methods get data from the parameter list, and then calculate these data.
3. Static code block
? Static modified code blocks have class-level priority, and are automatically executed when subclasses are loaded
Static variables and static functions modified by the static keyword use: class name.variable name; class name.function name
package com.star.demo11; public class Student {<!-- --> private String name; // private static variable private static int count; public Student(String name) {<!-- --> this.name = name; } public String getName() {<!-- --> return name; } public static int getCount() {<!-- --> return count; } public void setName(String name) {<!-- --> this.name = name; } // Static methods are called using [class name.] inside and outside the class public static void setCount(int count) {<!-- --> // Static variables are called by [class name.] inside and outside the class Student.count = count; } } -------------------------------------------------- -------------------------- package com.star.demo11; public class Main {<!-- --> \t public static void main(String[] args) {<!-- --> Student student1 = new Student("Zhang San"); student1. setCount(1); Student student2 = new Student("Li Si"); student2. setCount(2); Student student3 = new Student("Wang Wu"); student3. setCount(3); Student student4 = new Student("Mazi"); student4. setCount(4); //System.out.println("The total number of students created is: " + Student.count); System.out.println("The total number of students created is: " + Student.getCount()); System.out.println("The total number of students created is: " + student1.getCount()); System.out.println("The total number of students created is: " + student2.getCount()); System.out.println("The total number of students created is: " + student3.getCount()); System.out.println("The total number of students created is: " + student4.getCount()); } }
final final
final means “final, final” meaning. Once a variable is assigned a value, it cannot be reassigned.
The characteristics of final:
- A final modified instance variable must explicitly specify an initial value. Values can be assigned in constructors and normal code blocks, but only once
- The final modifier is often used with the static modifier to create class constant. You can assign a value in a static code block, but only once
- A final class cannot be inherited, no class can inherit any characteristics of a final class.
- The final method in the parent class can be inherited by subclasses, but cannot be overridden by subclasses.
**Note:** The main purpose of declaring a final method is to prevent the content of the method or class from being modified.
package com.star.demo12; public class Math {<!-- --> // Pi constant: static final private static final double PI = 3.1415926; // Find the area of the circle public static final double area(double radius) {<!-- --> //PI = 3;//report an error return PI * radius * radius; } // Find the area of the rectangle public double area(double width, double height) {<!-- --> return width * height; } } -------------------------------------------------- -------------------------- package com.star.demo12; //final modified class, this class cannot be inherited public final class Child extends Math{<!-- --> // Only non-final and static methods of the parent class can be overridden @Override public double area(double width, double height) {<!-- --> System.out.println("The method of the parent class is overridden, and the implementation of the parent class is called"); return super. area(width, height); } } //report an error, the final class cannot be inherited //class son extends Child{} -------------------------------------------------- -------------------------- package com.star.demo12; public class Main {<!-- --> public static void main(String[] args) {<!-- --> System.out.println("Area of a circle with a radius of 12.5: " + Child.area(12.5)); System.out.println("The area of a rectangle with a length of 20 and a width of 10: " + new Child().area(20,10)); } }
Result:
abstract abstract
abstract can modify classes and methods.
- The class modified by abstract is called abstract class, and the abstract class cannot be used to instantiate objects
- The method modified by abstract is called abstract method, and the abstract method has no method body
Note:
- A class cannot be both abstract and final
- Abstract methods cannot be declared final and static.
If a class contains abstract methods, then the class must be declared as an abstract class, otherwise a compilation error will occur
The sole purpose of declaring an abstract class is for future extensions to that class
Any subclass that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class.
If a class contains several abstract methods, then the class must be declared as abstract. An abstract class may not contain abstract methods.
package com.star.demo13; //A class with an abstract method must be declared as an abstract class with abstract public abstract class AbsClass {<!-- --> //Abstract method, cannot write method body{...} public abstract void fun1(); //Non-abstract method must write method body {...} public void fun2() {<!-- --> System.out.println("Abstract method of AbsClass: fun2"); } } -------------------------------------------------- -------------------------- package com.star.demo13; public class Child extends AbsClass{<!-- --> //Inheriting an abstract class must implement an abstract method that the abstract class does not implement @Override public void fun1() {<!-- --> System.out.println("The method implemented by the subclass: fun1"); } } -------------------------------------------------- -------------------------- package com.star.demo13; public class Main {<!-- --> public static void main(String[] args) {<!-- --> //AbsClass absClass = new AbsClass();//Abstract classes cannot be instantiated AbsClass absClass = new Child();//can be instantiated Child child = new Child(); absClass. fun1(); absClass. fun2(); child. fun1(); child. fun2(); } }
Four Java storage domains
Storage Domain | Features |
---|---|
Heap | Heap memory is used to store objects and arrays created by new. jvm has only one heap area (heap) shared by all threads. |
Stack | The first address of the array or object stored in the stack in the heap memory (32-bit 4 bytes, 64-bit 8 words Section), some basic types of variable data defined in the function Each thread contains a stack area, and the data (primitive types and object references) in each stack are private, and other stacks cannot access |
method area | method area is used to store data such as class information loaded by the virtual machine, static variables, and code compiled by the just-in-time compiler. Like the heap, the method area is shared by all threads. |
Constant pool | The constant pool refers to some data that is determined during compilation and saved in the compiled .class file. Contains constant values (final) of various basic types (such as int, long, etc.) and object types (such as String and arrays) defined in the code |
Java anonymous inner class
Anonymous class is a class that is not stored in variables and is no longer used after being used once
Including ordinary anonymous classes and anonymous inner classes
Inner class creation method: Outer class object.new inner class()
package com.star.demo07; public class User {<!-- --> private String name = "UserName"; String sex = "male"; protected int age = 20; public String address = "Earth"; \t // inner class class Inner{<!-- --> private String name = "InnerName"; String sex1 = "Female"; protected int age = 30; public String address = "Moon"; \t\t public void show() {<!-- --> System.out.println(name); System.out.println(sex); System.out.println(sex1); System.out.println(age); System.out.println(address); } } public void show() {<!-- --> System.out.println(name); System.out.println(sex); //System.out.println(sex1); System.out.println(age); System.out.println(address); } } -------------------------------------------------- -------------------------- package com.star.demo07; import com.star.demo02.User.Inner; public class Main {<!-- --> public static void main(String[] args) {<!-- --> User user = new User(); user. show(); Inner inner = user. new Inner(); inner. show(); } }
JAVA is interface-oriented
Interface is a new type, different from class, but similar to class
The interface is equivalent to a very special abstract class, as long as the public****abstract function*and*constant****
The interface has no construction method, so the interface cannot be instantiated, but can only be implemented, so the subclass uses the [*implements*] keyword to implement the interface
A subclass can implement multiple interfaces (multiple implementations)
Interface features:
- Each method in the interface is also implicitly abstract, and will be implicitly designated as public abstract (it can only be public abstract, and other modifiers will report an error).
- An interface can contain variables, but the variables in the interface will be implicitly designated as public static final variables (and can only be public, and a compilation error will be reported if modified with private).
- The method in the interface cannot be implemented in the interface, only the class that implements the interface can implement the method in the interface.
The default keyword is used to implement the default method
default void bite() {<!-- --> System.out.println("Default implementation method"); }
package com.star.demo14; public interface Mather {<!-- --> // can only be public static final, and must be assigned public static final String firstname = "Tao"; public static final String lastname = "Qi"; public static final String sex = "female"; // Default is public void beautify(); default void bite() {<!-- --> System.out.println("Default implementation method"); } } -------------------------------------------------- -------------------------- package com.star.demo14; public interface Father {<!-- --> // can only be public static final, and must be assigned public static final String firstname = "too"; public static final String lastname = "Shi Ci"; public static final String sex = "male"; // Default is public void strong(); } package com.star.demo14; public class Son implements Father, Mather {<!-- --> public String firstname; public String lastname; public String sex; public Son() {<!-- --> this.firstname = Father.firstname + Mather.firstname; this.lastname = "Qi"; this.sex = Math.random() + 1 > 0 ? "Male" : "Female"; } @Override public void beautify() {<!-- --> System.out.println("beautiful"); } @Override public void strong() {<!-- --> System.out.println("Strong"); } } -------------------------------------------------- -------------------------- package com.star.demo14; public class Main {<!-- --> public static void main(String[] args) {<!-- --> Mather mather = new Son(); Father father = new Son(); Son son = new Son(); \t\t mather.beautify();//Only Mather's beautify method father.strong();//Only Father's strong method son.beautify();//Not only the beautify method son.strong();//There is also a strong method } }
Exercise:
1. Student performance statistics. There are 10 students in a class, and the scores of 10 students are entered in a loop, and the statistics are displayed.
Tip: Create a student class (student name + grade)
? 1. Display student name + grade
2. Count the total score, average score and pass rate of the class
? 3. Show failed student name + grade
2. Simulate a game of poker, 54 cards,
Colors are: Hearts, Clubs, Spades, Diamonds,
Card values are: A, 1-10, J, Q, K
Analyze which classes exist and create 54 card objects.
Tip: For each card: Card(color(value, text)), Value(value(number, text))