JAVA中的类和对象
练习题
设计出物品这种类
类名:Item
物品有如下属性:
名字 name 类型是字符串String
价格 price 类型是整型 int
内存 ram 类型是整型 int
重量 weight 类型是整型 float

试着在iPad Air上增加2G内存跟价格调整500

创建3件具体物品

名称价格内存重量
小米826996284.5
iPad Air69994300.5
MacBook9500161340.2
public class Item {

String name;
int price;
int ram;
float weight;

void buy() {
        System.out.println("购买哪款?");
    }
    
int getArmor(){
        return ram;    //获取内存
    }

void addRam(int RAM){
        ram=ram + RAM;    //在原来的基础上增加内存
    }

void addPrice(int Price){
    price=price + Price;    //在原来的基础上增加金额
}
public static void main(String[] args) {
    
    
    
    Item phone = new Item();
    Item iPad = new Item();
    Item PC =new Item();
    
    phone.name ="小米8";    //名称
    phone.price =2699;      //金额
    phone.ram =6;           //内存
    phone.weight =284.5f;   //重量
    
    iPad.name = "iPad Air";
    iPad.price =6999;
    iPad.ram =4;
    iPad.weight =300.5f;
    
    PC.name ="Macbook Pro";        
    PC.price =9500;
    PC.ram =16;
    PC.weight =1340.2f;
    
    iPad.buy();
    iPad.addRam(2);
    iPad.addPrice(500);
    float ram =iPad.getArmor();
    System.out.println("我要高配的iPad Air");
    System.out.println(iPad.name+"价格:"+iPad.price+"  内存:"+ram+"  重量:"+iPad.weight);
    }
}
Last modification:April 10th, 2019 at 12:28 am
If you think my article is useful to you, please feel free to appreciate