Java classes and objects

1, Basic concepts of class objects:

(1). Object oriented programming idea:

OOA (object oriented program analysis)

OOD (object oriented programming)

OOP (object oriented programming)

(2). Classes and objects:

A. a collection of things or things with the same attributes and characteristics

Such as: humans, cars, animals

b. a specific individual in a class is called an object

For example: Animal Labrador

(3). attribute

A. common features contained in a class

For example, animals can bark and eat; Humans have names, ages, and genders

b. any data type can be regarded as an attribute

Such as basic data type and reference data type

c. There is a modifier in front of the attribute to control the access permission (I will explain it later here. At present, it is decorated with the public keyword)

d. how to use attributes

public class Person(){
    //The attribute can be given or not given an initial value, and can be assigned after the object is created
    public String name;//Initial name is null
    public int age = "18";//Age
    public String sex = "female";//Gender   
}
public class Test(){
    public static void main(){
        Person p = new Person();//create object
        //Value
        int age = p.age;
        //assignment
        p.age = 10;
        //Change gender to male
        p.sex = "male";
        //Output operation
        System.out.println(p.name+"this year:"+p.age+"year,It's a"+p.sex);
    }
}

2, Method:

Significance of method: the use of method is convenient for us to reuse complex code

Significance of parameters: the existence of parameters enhances the flexibility of the method

Application scenario of return value: if others need to continue using the results generated in the method, they must use the return value for processing

There are four types:

(a). No return value no parameter:

public class Animal{
    public void voice(){
        System.out.println("Animals are barking");
    }
    public void eat(){
        System.out.println("Animals eat");
    }
    public static void main(String[] args) {
        Animal a = new Animal();//create object
        a.voice();//Object call method
        a.eat();
    }
}

(b). No return value with parameters:

public class Test{
    public void test(int a,int b,String s){//Parameter list parameter
        System.out.println("The first parameter is passed:"+a);
        System.out.println("The second parameter is passed:"+b);
        System.out.println("The third parameter is passed:"+s);
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.test(10,1,"a");
    }
}

(c). With return value and no parameter:

public class Test{
    public int tellAge(){
    int a = 10;
    return a;//The return value must be given a specific value
    }
    public static void main(String[] args) {
        Test t = new Test();
        int age = t.tellAge();
        System.out.println(age);//10
    }
}

(d). Parameters with return values:

public class Test{
    public int add(int a,int b){
       int sum = a + b;
       return sum;
    }
    public int sub(int x,int y){
        int value = x - y;
        return value;
    }
    public int mul(int x,int y){
        int value = x * y;
        return value;
    }
    public static void main(String[] args) {
        Test t = new Test();
        int sum = t.add(1,2);
        int x = t.sub(sum,4);//You can pass a variable name, but it must have a value
        int v = t.mul(x,2);
        System.out.println(v);
    }
}

3, Global and local variables:

(a). Global variables:

Properties:

        ​ 1. Global variables can be used directly in any method in the current class

        ​ 2. Changes to global variables in the methods of an object will synchronously affect other methods of the same object

(b). Local variables:

Variables and parameters defined inside the method:

         1. Local variables can only act in the current method

​         2. Changes to local variables by one method do not affect other methods

be careful:

(1). Both global and local variables cannot have the same name when used internally

(2). When the local variable and the global variable have the same name, the local variable is used by default. If you want to use the global variable, you need to use this Attribute name

public class Person {
    public int age = 20;
    //Used to assign values to attributes
    public void setAge (int age){
        this.age = age;
    }
    public void test(){
        int age = 10;
        System.out.println("seven:"+this.age);//Demonstrative pronoun: refers to the object to be produced in the future
    }
    public static void main(String[] args) {
        Person p = new Person();
        p.test();//age = 20
        p.setAge(10);
        System.out.println(p.age);//age = 10
    }
}

I will regularly publish relevant knowledge about Java. If there are errors, please point out them. If you feel good, please like them and pay more attention to them. Thank you!!

 

Keywords: Java

Added by lawnninja on Sat, 18 Dec 2021 04:44:58 +0200