Java: Class and Object and Source File Declaration Rules

statement

1) Part of the content of this article is compiled from the information on the Internet. If you accidentally infringe on everyone's rights, you can still contact the blogger to delete it.

2) The blogger is Mengxin on the road. If there is any inappropriateness in the article, please point it out and make progress together, thank you.

class

concept

A class is a template that describes the behavior and state of a class of objects.

Use the following simple class to understand the definition of classes in Java:

public class Dog{
    
    
  String name;                //狗的名字
  String color;               //狗的颜色
  int age;                    //狗的年龄

  void eating(){
    
                  //吃饭
  }

  void sleeping(){
    
                //睡觉
  }
}

A class can contain the following types of variables:

Local variables: Variables defined in methods, construction methods, or code 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, constructors, and code blocks of a specific class.

Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types.

A class can have multiple methods. In the above example: eating() and sleeping() are both methods of the Dog class.

Construction method

Every class has a constructor. If no constructor is explicitly defined 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.

The following is an example of a construction method:

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

Object

concept

An object is an instance of a class, with state and behavior. For example, a dog is an object, its status includes: color, name, breed; behavior: wagging, barking, eating, etc.

Software objects also have states and behaviors. The state of the software object is the attribute, and the behavior is reflected by the method.

In software development, methods operate on changes in the internal state of objects, and the mutual calls of objects are also done through methods.

Create object

Objects are created based on classes. In Java, use the keyword new to create a new object. Three steps are required to create an object:

Declaration : Declare an object, including the object name and object type.

Instantiation : Use the keyword new to create an object.

Initialization : When using new to create an object, the constructor will be called to initialize the object.

The following is an example of creating an object:

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

Compile and run the above program, it will print out the following result:

小狗的名字: Tom

Access instance variables and methods

Access member variables and member methods through the created object, as shown below:

/* 实例化对象 */
Object referenceVariable = new Constructor();
/* 访问类中的变量 */
referenceVariable.variableName;
/* 访问类中的方法 */
referenceVariable.methodName();

Example
The following example shows how to access instance variables and call member methods:

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

Compile and run the above program to produce the following result:

小狗的名字: Tom
小狗的年龄: 2
变量值 : 2

Source file declaration rules

In the last part of this section, we will study the declaration rules of source files. When defining multiple classes in a source file, and there are import statements and package statements, pay special attention to these rules.

There can only be one public class in a source file

A source file can have multiple non-public classes

The name of the source file should be consistent with the class name of the public class. For example, if the class name of the public class in the source file is Employee, the source file should be named Employee.java.

If a class is defined in a package, then the package statement should be the first line of the source file.

If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.

The import statement and package statement are valid for all classes defined in the source file. In the same source file, different packages cannot be declared for different classes.

Classes have several access levels, and classes are also divided into different types: abstract classes and final classes.

In addition to the several types mentioned above, Java also has some special classes, such as inner classes and anonymous classes.

Thinking

  1. How is an object converted into a string?

Method 1: Use Object#toString() method
Method 2: Use type conversion (String) object method
Method 3: Use String.valueOf(Object)
For details, see: java object is converted to String type

  1. How should one object be equal to another object?

To determine whether an object is equal to another object, you should call the equalTo method instead of ==

Guess you like

Origin blog.csdn.net/weixin_46263782/article/details/107627566