java: class related

class

public class Dog{
    
    
  String breed;
  int age;
  String color;
  void barking(){
    
    
  }
 
  void hungry(){
    
    
  }
 
  void sleeping(){
    
    
  }
}

Class method

public class Main {
    
    

    public static void main(String[] args) {
    
    
    	// 方法调用
        myMethod();
    }
    // 方法
    public static void myMethod () {
    
    
        System.out.print("你好");
    }
}

Variable length parameter ..., can only be used as the last parameter

public class Main {
    
    

    public static void main(String[] args) {
    
    
        myMethod("1","2");
    }
    public static void myMethod (String... args) {
    
    
        System.out.println("你好" + args[0]);
    }
}

Class construction method (equivalent to initialization)

Every class has a constructor. If you do not explicitly define a constructor for the class, the Java compiler will provide a default constructor for the class.

When creating an object, at least one constructor must be called. The name of the constructor must have the same name as the class, and a class can have multiple constructors.

public class Puppy{
    
    
    public Puppy(){
    
    
    }
 
    public Puppy(String name){
    
    
        // 这个构造器仅有一个参数:name
    }
}

Create an object

public class Puppy{
    
    
   public Puppy(String name){
    
    
      //这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
   public static void main(String[] args){
    
    
      // 下面的语句将创建一个Puppy对象
      Puppy myPuppy = new Puppy( "tommy" );
   }
}
// 小狗的名字是 : tommy

Access instance variables and methods

public class Puppy{
    
    
   int puppyAge;
   public Puppy(String name){
    
    
      // 这个构造器仅有一个参数:name
      System.out.println("小狗的名字是 : " + name ); 
   }
 
   public void setAge( int age ){
    
    
       puppyAge = age;
   }
 
   public int getAge( ){
    
    
       System.out.println("小狗的年龄为 : " + puppyAge ); 
       return puppyAge;
   }
 
   public static void main(String[] args){
    
    
      /* 创建对象 */
      Puppy myPuppy = new Puppy( "tommy" );
      /* 通过方法来设定age */
      myPuppy.setAge( 2 );
      /* 调用另一个方法获取age */
      myPuppy.getAge( );
      /*你也可以像下面这样访问成员变量 */
      System.out.println("变量值 : " + myPuppy.puppyAge ); 
   }
}
/*
小狗的名字是 : tommy
小狗的年龄为 : 2
变量值 : 2
*/

Initialization order of objects

1. Class loading
concept: the process of reading and loading class files into JVM memory

2. The timing of class loading:
1. Create objects of the class
2. Call static properties or methods of the class
3. When the main method is executed, the class where the main method is located will be loaded
4. Call the class through reflectionClass.forName("包名.类名")

3. The initialization sequence
of the object 1. The class where the object is located is loaded
. Static properties and static code blocks are executed (the order of execution is from top to bottom).
Features:

  • Static variables are initialized when the class is loaded, and space is allocated only once in memory
  • The static code block is executed when the class is loaded and only executed once (the class will only be loaded once, that is, when you create two objects of the class, the class is actually loaded only once)
    2. The object is created to
    execute the instance properties and code Block (execution order from top to bottom)
    execution construction method

Member of class

  • Properties (instance properties, static properties)
  • Method (instance method, static method)
  • Construction method
  • Code block
  • Static code block
class User {
    
    
	// 实例属性
	String name = "张三";
	// 静态属性
	static String hobby = "吃饭";

	public void a () {
    
    
		System.out.println("实例方法");
	}

	public static void b () {
    
    
		System.out.println("静态方法");
	}
	
	public User () {
    
    
		System.out.println("构造方法");
	}

	{
    
    
		System.out.println("代码块");
	}

	static {
    
    
		System.out.println("静态代码块");
	}
}

Execution order: static code block -> code block -> construction method

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113483990