类 对象和引用 实例

类和对象

1.类 对象 实例和引用

先举个例子,比如要制造一辆汽车。先得了解这个汽车的功能如move(),stop()等,等你构思好了这辆汽车的功能后,开始想这辆汽车的外观如颜色,大小等,我们称为属性。这些功能和属性的集合的组合就是一个类,一个要生产汽车的概念模型。有了这个模型我们才能制造一辆汽车。而当实际制造出来一辆汽车就相当于实例了一个对象。这个被制造出来的汽车就是一个对象。而你给这辆汽车起一个专属的爱称“大黄”,“大黄”就是这个对象的引用。你叫“大黄”就专指这辆车。下面通过一段代码来理解上面那段话。

class Car{
    String color;
    int size;
    public Car(String color,int size){
    System.out.println("color:");
    System.out.println("size:");
    }

    public void move(){
    System.out.println("老司机开车了!");
    }
    public void stop(){
    System.out.println("stop!");
    }

}
public class Test{
    public static void main(String[] args){
        Car dahuang = new Car("yellow",80);
        dahuang.move();
        dahuang.stop();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_41706670/article/details/80010961