java-class
1. First introduce the concepts of Java objects and Java classes:
Object : An object is an instance of a class (the object is not looking for a girlfriend), has state and behavior. For example, a dog is an object, its states are: color, name, breed; behaviors are: wagging tail, barking, eating, etc.
Class : A class is a template that describes the behavior and state of a class of objects.
The boy (boy) and girl (girl) in the figure below are classes, and each specific person is an object of this class: the
car in the figure below is a class (class), and each specific car is an object of this class. The object of the car class (object), the object contains the color, brand, name, etc. of the car.
Classes can be thought of as templates for creating Java objects.
Create a simple class through the above figure to understand the definition of a class in Java:
public class Dog {
String breed;
int size;
String colour;
int age;
void eat() {
}
void run() {
}
void sleep(){
}
void name(){
}
}
A class can contain variables of the following types:
Local variables: Variables defined in methods, constructors, or statement blocks are called local variables. Variable declaration and initialization are in the method, after the method ends, the variable will be automatically destroyed.
Member variables: Member variables are variables defined in the class, outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods in the class, constructors, and statement blocks of specific classes.
Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static type.
A class can have multiple methods, in the above example: eat(), run(), sleep() and name() are all methods of the Dog class.
The relationship between local variables and member variables:
2 types of composition
Defined by:
import 语句;
类定义{
成员定义
构造方法
成员方法
}
2 Java Permission Modifiers
2.1 Access Modifiers
①Modification class: public, default modifier (I will talk about why classes can only use these two modifiers later)
② Modified member methods and member variables: public, private, default modifiers, protected
2.2 Limited scope
-
private (current class access rights)
Members decorated with private can only be accessed inside the current class. Often used to modify member variables (encapsulation). -
default (package access permissions)
Default permission modifier. Members or external classes decorated with default can be accessed by other classes under the same package. -
protect (subclass access rights)
members modified with protect can be accessed by other classes in the same package, and can also be accessed by subclasses in different packages. It is often used to modify methods that want to be overridden by subclasses (override). -
public (public access rights)
Members or external classes decorated with public can be accessed by all classes.
3 The reason why the access modifier of the class does not use private and protected
(1) Reasons for not using private
After using private modification, it will not be instantiated and its members will not be available, so the created class can be said to be free
(2) Reasons for not using protected
First of all, we need to know its access scope. If we want to use this class, we must inherit it, but inheriting it must be its subclass to inherit, which creates an unrealizable condition