public class Mobile {//class
public String brand;//属性 public是修饰符
public int weight;//属性
public String getBrand (){//method 方法 String 是返回值类型,一般方法 和构造方法的不同之处。
return brand;
}
public Mobile(String brand,int weight){//constructor 构造方法
this.brand = brand;
this.weight = weight;
}
public static void main(String[] args)//main method,程序入口
{
//Mobile mobile = new Mobile();//无参构造器
Mobile mobile = new Mobile("samsumg",200);//有参数的对象创建。构造器
System.out.println("手机品牌"+mobile.getBrand());
System.out.println(mobile.brand);//对象方法和对象属性的访问用 点 操作符
System.out.println(mobile.weight);
}
}
java,method,contructor,class,属性 程序
最新推荐文章于 2022-08-01 19:35:42 发布