12, Java objects and classes

Object (Object)

In object-oriented thinking, objects mean to refer to a particular object, no matter what the object can be an object, if a thing is an object, then the object must have its own parameters, that is, its own certainly some real-world conditions, such as:

A box: it will certainly be a long, wide, high, these three parameters.

A person: it will certainly be age, gender, height. . And other conditions.

  So that an object must have its own parameters. But if you want to define an object as a class, you must be abstract.

abstract

Abstract is a group of similar objects, their similarities extraction composition class.

Class (class)

Class is a class of things in common, for example, will have multiple boxes length, width and height, which is all the boxes have the features they have in common so drawn on the composition of the target.

  Then the class is an abstract object, and the object is an instance of another class.

Class definition

A class which can have its own attributes, is a member variable, you can also have their own action, which is the method, so that it can constitute a class. such as:

Define a student class:

//定义一个学生类
public class Student{
    //成员变量
    int id          //学生的学号     
    String name;    //学生的姓名
    int age;        //学生的年龄
    
    //学习方法
    public void study(){
        System.out.println("学习");
    }
    
    //上学方法
    public void goToSchool(){
        System.out.printLn("去上学");
    }
    
    //构造方法,这是每一个类自动默认就有的方法
    //自己可以不用写
    public Student(){
        
    }
    
}

Creating a student objects

Now, with a class of students, the student is the student's class abstract, you can create objects with the:

//再创建一个新的类,专门用来测试用的
public class Test{
    public static void main(String[] args){
        //通过调用这个类的构造方法来创建一个学生对象
        Student student = new Student();
        //通过这个对象来调用学习方法
        student.study();
        //输出的结果:学习
    }
}

An object reference to another object

//创建一个铅笔类
public class Pencle{
    String pencleName;  //铅笔的名字
}
//在刚才的学生类当中加一个属性
public class Student{
    //成员变量
    int id          //学生的学号     
    String name;    //学生的姓名
    int age;        //学生的年龄
    Pencle pencle;  //一个类的对象也可以为另一个类的属性
    
    //学习方法
    public void study(){
        System.out.println(name+"学习用:"+pencli.pencleName);
    }
    
    //上学方法
    public void goToSchool(){
        System.out.printLn("去上学");
    }
    
    //构造方法,这是每一个类自动默认就有的方法
    //自己可以不用写
    public Student(){
        
    }
}
//再一次重写刚才的Test类
public class Test{
    public static void main(String[] args){
        //通过调用这个类的构造方法来创建一个学生对象
        Student student = new Student();
        //可以通过 对象.属性给对应的属性赋值
        student.id = 1;
        student.name="张三";
        student.age=18;
        //创建一个铅笔对象
        Pencle pencle = new Pencle();
        pencli.pencleName = "彩铅";
        //把这个对象赋给student类里面的铅笔对象
        student.pencle = pencle;
       
        //通过这个对象来调用学习方法
        student.study();
        //输出的结果:张三学习用彩铅
    }
}

  The above content is a reference to a very simple class and assignment. I wrote again basically can be understood.

  The following chapters give you a detailed analysis of the situation of memory objects.




Details determine success or failure!

Personal humble opinion, if not, ask righting!

Guess you like

Origin www.cnblogs.com/xdtg/p/12343497.html