kind
Classes are models that determine the characteristics (properties) and behaviors (methods) an object will have
Class Features
class is the type of object
a collection of objects with the same properties and methods
Properties - various characteristics of an object, each property of each object has a specific value
Method - the operation performed by the object
A class is an abstract concept, just a template, for example: The "mobile phone" object is a concrete entity that can be seen and touched
how to define a class in java
1. The importance of classes: All java programs are organized in classes ( classes )
2. What is a class? Classes are models that determine the characteristics (properties) and behaviors (methods) an object will have
3, the composition of the class: properties and methods
4. Steps to define a class
a. Define the class name
b. Write the properties of the class
c. How to write a class
Java object
1. Create an object
class name object name = new class name ();
Telphone phone=new Telphone();
2. Objects of use
Attributes of referenced objects: objectname.attributes
Phone.screen=5;// Assign screen familiarity to 5
Methods of referencing objects: objectname.methodname ( )
Phone.sendMessage();// Call the sendMessage() method
public class TelPhone { // What is the property (member variable) float screen; float cpu; float mem; // What does the method do void call(){ System.out.println( "telphone has the function of making calls" ); } void sendMessage(){ System.out.println( "screen:"+screen+"cpu:"+cpu+"mem:"+mem+"telphone has the function of sending text messages" ); } } public class InitailTelPhone { public static void main(String[] args){ // Create an object of the class TelPhone phone= new TelPhone(); // Call the class method phone.sendMessage(); // Assign the instance variable phone.screen =5.0f ; phone.cpu=1.4f; phone.mem =2.0f ; // call the class method phone.sendMessage() again; } }
member variables and local variables in java
1. Member variables
Defined in the class to describe what the object will have
The methods of this class can be used, or the methods of other related classes can also be used
2. Local variables
Defined in the method of the class, temporarily save the data in the method
can only be used in the current method
Difference between member variable and local variable
1. Different scopes
The scope of a local variable is limited to use inside the method in which it is defined
The scope of member variables is available within the entire class
2, the initial value is different
Java will give a member variable an initial value, the initial value is 0
Local variables have no initial value
3. In the same method, local variables with the same name are not allowed
4. In different methods, there can be local variables with the same name
5. When two types of variables have the same name, local variables have higher priority
Constructor in java
1. Use the new+ constructor to create a new object
Tips: When creating an object, new+ is actually a constructor
2. A constructor is a method defined in a java class to initialize an object
3. The constructor has the same name as the class and has no return value
No-argument constructor
main:
Telphone phone=new Telphone() ;// Constructor method name
class Telphone:
public Telphone(){
System.out.println(" No parameter constructor executed! ");
}
Use of parameterized constructors
main:
Telphone phone=new Telphone( 4.0f,2.0f,2.0f ) ; // The name of the constructor, the parameters of the constructor are in parentheses
class Telphone:
public Telphone(float newScreen,float newCpu,float newMen){
screen=newScreen;
cpu=newCpu;
mem=newMen;
}
public class InitailTelPhone { public static void main(String[] args){ // Create a class object through a parameterless constructor TelPhone phone= new TelPhone(); // Create a class object through a parameterized constructor and give it to The instance variable in the object is assigned the initial value TelPhone phone2= new TelPhone(5.0f,2.0f,3.0f ); } } public class TelPhone { // Define member variables float screen; float cpu; float mem; public TelPhone(){ System.out.println( "Constructor with no parameters executed" ); } // Construction method with parameters // The parameters in parentheses and member variables are different public TelPhone( float newScreen, float newCpu, float newMen){ // Assign the initial value to the member variable screen= newScreen; cpu=newCpu; mem = newMen; System.out.println( "Constructor with parameters executed" ); } }
When no constructor is specified, the system will automatically add a parameterless constructor
When there is a specified constructor, whether it is a constructor with or without parameters, a constructor without parameters will not be automatically added.
Constructor overloading: multiple methods with the same method name but different parameters, the call will automatically select the corresponding method according to different parameters
The constructor can not only assign values to the properties of the object, but also ensure that a reasonable value is assigned to the properties of the object.
// Construction method with parameters // There is a difference between the parameters and member variables in parentheses public TelPhone( float newScreen, float newCpu, float newMen){ // Assign initial values to member variables, and ensure a reasonable value if (newScreen<3.5f ){ System.out.println( "There is a problem with the parameter you entered, automatically assign 3.5" ); screen=3.5f; }else{ screen=newScreen; } cpu=newCpu; mem = newMen; System.out.println( "Constructor with parameters executed" ); }
Static variables used by Static
Members modified by static are called static members or class members. It belongs to the entire class, not an object, that is, shared by the objects of the class. Static members can be accessed directly using the class name or by using the object name.
Use static to modify variables, methods, and code blocks.
public class HelloWorld{ // Define static variable static String name="liuYun" ; int a=10 ; public static void main(String[] args){ System.out.println(HelloWorld.name); // You can use the class name to directly access System.out.println(name); // This way you can directly access HelloWorld hello= new HelloWorld(); System.out.println(hello.a); // Non-static variable is accessed by object name } }
Static methods used by Static
1. A static method can directly call static members of the same class, but cannot directly call non-static members. If you want to call a non-static variable in a static method, you can access the non-static variable by creating an object of the class
2. In ordinary member methods, you can directly call non-static variables and static variables of the same kind
3. Non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects
public class HelloWorld{ // Define static variable static int score1=86 ; static int score2=92 ; // Define static method sum public static int sum(){ int sum; sum=score1+score2; return sum; } public static void main(String[] args){ int sum=HelloWorld.sum(); // Call the static method System.out.println("Total score: "+ sum); } }
Static initialization block used by Static
Data assignment can be done through initialization block in Java
public class HelloWorld{ String name; // Define a member variable // Assign value to member variable through initialization block { name="liuYun"; } }
If an initializer block is decorated with static , it is called a static initializer block.
Special attention is required:
The static initialization block is only executed when the class is loaded, and only once. At the same time, the static initialization block can only assign values to static variables.
Ordinary member variables cannot be initialized.
public class HelloWorld{ String name; String sex; static int age; // Constructor public HelloWorld(){ System.out.println( "Initialize name by constructor" ); name="liuYun"; } // initialization block { System.out.println( "Initialize sex with initialization block" ); sex="男"; } // static initialization block static { System.out.println( "Initialize age with static initialization block" ); age=29; } public void show(){ System.out.println( "Name: "+name+", Gender: "+sex+", Age: "+ age); } public static void main(String[] args){ HelloWorld hello=new HelloWorld(); hello.show(); } }