认识对象(一)

建立对象

class Clothes{
String color;
char size;
}

建立一个主类,主类名和java文件名相同

public class Field {

    public static void main(String[] args) {
       Clothes sun=new Clothes();//建立四个对象
       Clothes spring=new Clothes();
       Clothes sp2=spring;  //建立一个对象使他 等于spring
       Clothes sun2=new Clothes();

进行赋值

sun.color="red";
       sun.size='S';
       spring.color="green";
       spring.size='M';
       sp2.color="blue";
       //因为sp2是spring的别名,所以他们属于用一个数据,当改其一就相当于改两个;
       sun2.color=sun.color; 
       //sun2是重新建立的一个对象分别给其赋值,他们属于两个对象
       sun2.size=sun.size;
           //sun2.color="green";

输出结果:

 

猜你喜欢

转载自blog.csdn.net/leaflifeli/article/details/81087805