Java_____ classes and objects

1. Class: An abstract concept that represents a general term for a class of things

Humans: Attributes - Name, Age, Gender
, Behavior - Food, Clothes, Sleeping
, etc. Class Name: Indicates the common features and behaviors of a class of things
Object : an instance of a class
insert image description here

Object-oriented : c++ Java Everything is an object for easy development, loosely coupled
Process-oriented : c Everything is an independent method, with high performance (embedded software: single-chip microcomputer)

how to define a class

class Person{
    
    
}

Human { several properties... several methods/behaviors } Note : 1. Any source file has one and only one main class (public class) and the source file name needs to be consistent with the main class




insert image description here

2. Class naming

The naming of classes and the naming of source files adopts the big camel case notation: the first letter of all words is all capitalized
. The naming of variables uses the small camel case notation

class Person;
class ChinaPerson;

3. Instantiation of the class

A source file can have several classes (ordinary classes) 0-n
Defining a class just specifies the attributes and behaviors of a class of objects
After having a class, objects can be generated according to the class ( class instantiation )
insert image description here

4. The keyword new to generate an object

The main method main exists in the main class
insert image description here

5. What is contained in the class

What a class contains: properties, methods/behaviors, classes, interfaces, code blocks
Properties in a class - member variables or properties
Methods in a class - member placement or instance methods

Question 1 : How to use the properties of the class through the object

insert image description here
To access properties and methods by reference data types belong to the **"."** operator

Question 2: About property default values

Basic data type: default value
The member variables in the class are related to the object. When an object is generated, the memory will be opened up and space allocated

//类来产生对象
public class Test {
    
    
    //成员变量(与对象有关)
    int a;
    public static void main(String[] args) {
    
    
        //类的实例化——>产生一个Person类的对象
       /* Person per = new person();
        per.name = "张三";
        per.age = 18;
        per.sex = "男";*/

        Test 狗剩 = new Test();
      //  类  给这个对象起的名字  = 类的对象
        System.out.println(狗剩.a);
    }
}

insert image description here

6. Default value of reference data type: null

public class Test {
    
    
    //成员变量(与对象有关)
    int a;
    public static void main(String[] args) {
    
    
        //类的实例化——>产生一个Person类的对象
       Person per = new Person();
       /* per.name = "张三";
        per.age = 18;
        per.sex = "男";*/

      /*  Test 狗剩 = new Test();
      //  类  给这个对象起的名字  = 类的对象*/
        System.out.println(per.name);
    }
}

insert image description here

7. You can assign values ​​to properties at the time of definition

//class 定义一个类
class Person{
    
    
    //属性
    String name = "张三";
    int age;
    String sex;
    //行为
    void eat(){
    
    };
    void sleep(){
    
    };

}
//类来产生对象
public class Test {
    
    
    //成员变量(与对象有关)
    int a;
    public static void main(String[] args) {
    
    
        //类的实例化——>产生一个Person类的对象
       Person per = new Person();
       /* per.name = "张三";
        per.age = 18;
        per.sex = "男";*/

      /*  Test 狗剩 = new Test();
      //  类  给这个对象起的名字  = 类的对象*/
        System.out.println(per.name);
    }
}

insert image description here

8. Member methods in a class

//class 定义一个类
class Person{
    
    
    //属性
    int age;
    String sex;
    //行为
    void eat(){
    
    };
    void sleep(){
    
    };
    void show(){
    
    
        System.out.println("My name is:"+name+",My age is:"+age);
    }

}
//类来产生对象
public class Test {
    
    
    //成员变量(与对象有关)
    int a;
    public static void main(String[] args) {
    
    
        //类的实例化——>产生一个Person类的对象
       Person per = new Person();
       Person per1 = new Person();
       per.name = "张三";
        per.age = 20;
       per1.name = "柳飘飘";
       per1.age = 101;
      
        per.show();
        per1.show();
    }
}

insert image description here
Summary : The member variables and member methods in the class are strongly related to the object and can only be used after there is an object! ! !

2.static keyword

static{
    
    
1.修饰变量(类属性)
2.修饰方法(类方法)
3.修饰代码块(静态块)
4.修饰内部类
}

1.Static modification attribute -> class attribute

When an attribute in a class, all objects of the class have the same value (eg: nationality), this attribute is not related to the specific object, but is related to the associated class, the attribute is modified with static, indicating the class attribute, in JVM method area storage.
Method area: store all static variables and constants
static

class Person{
    
    
    //类属性
    static String country;
    }
//类来产生对象
public class Test {
    
    
    //成员变量(与对象有关)
    int a;
    public static void main(String[] args) {
    
    
        //类的实例化——>产生一个Person类的对象
       Person per = new Person();
       Person per1 = new Person();
       per.country = "台湾";

        System.out.println(per1.country);

    }
}

insert image description here

2. Static modified variables are called class static variables or class attributes

Directly use the class name to access, object access is not recommended
insert image description here

3. The static keyword must not appear in the method in Java

insert image description here

4. Constants: variables that cannot be modified once defined, modified with the final keyword, generally used in conjunction with static

//class 定义一个类
class Person{
    
    
    //常量,定义时必须初始化,并且之后无法修改
    final static String COUNTYR = "中国";
    }
public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Person.COUNTYR);
    }
}

insert image description here
Constant naming convention:
All words are capitalized! ! ! Multiple words can be separated by _

5. Static methods

The static modification method used in the class-----Can the class method (not related to the object)
call the static method in the member method? Yes
class object
can call member method in static method? No
object class
See that static has nothing to do with the object, but with the class itself
. The properties modified by static are stored in the JVM method area, and all objects share this variable (all objects in this class have one and only one)

Note: There is only one main class (public class) in *.java and there can be several common classes
. The name of the main class must be consistent with the name of the source file

Guess you like

Origin blog.csdn.net/biteqq/article/details/122737412