构建超市商品类,学生类,Father和child类,并测试

1.超市商品类

class Goods{
    private int num;
    private String name;
    private double price;
    Goods(){super();}
    public Goods(int num,String name){
        super();
        this.num=num;
        this.name=name;
    }
    public int getNum(){return num;}
    public void setNum(int num){this.num=num;}
    public String getName(){return name;}
    public void setName(String name){this.name=name;}
    public double getPrice(){
        int NAME=getNum();
        switch (NAME){
            case 001:price=20000;
            break;
            case 002:price=30000;
            break;
            default: price=150000;
        }
        return price;
    }
    public void inforshow(){
        System.out.println("G00ds num="+getNum()+",name="+getName()+",price="+getPrice());
    }
}
class GoodsShop{
    private double money=0;
    public double sellGoods(Goods car){
        double price=car.getPrice();
        return money=money+price;
    }
    public double getMoney(){
        return money;
    }
}
public class T4{
    public static void main(String[] args){
        GoodsShop goodsShop=new GoodsShop();
        Goods g1=new Goods(001,"手机");
        goodsShop.sellGoods(g1);
        Goods g2=new Goods(002,"电脑");
        goodsShop.sellGoods(g2);
        g1.inforshow();
        g2.inforshow();
        double counMoney= goodsShop.getMoney();
        System.out.println("商品出售的价格是:"+counMoney);
    }
}

 运行结果

2.学生类

class student {
    private String name;
    private double score;
    public student(){

    }
    public student(String name,double score){
        this.name=name;
        this.score=score;
        System.out.println("姓名:"+name);
        System.out.println("成绩:"+score);
    }
    public String getName(){
        return name;
    }

    public double getScore() {
        return score;
    }

    public void setName(String name){
        this.name=name;
        System.out.println("姓名:"+name);
    }
    public void setScore(double score){
        this.score=score;
        System.out.println("成绩:"+score);
    }
}
public class T1 {
    public static void main(String[] args){
        student stu1=new student();
        stu1.setName("lijun");
        stu1.setScore(100);
        student stu2=new student("lili",99);
    }
}

运行结果

3.Father和child类

class Father {
    private String name = "lijun";
    class Child {
        public void introFather()
        {
            System.out.println(name);
        }
    }
    public void introFather() {
        Child get = new Child();
        get.introFather();
    }
}
public class T1 {
    public static void main(String[] args){
        Father in=new Father();
        in.introFather();
    }
}

 运行结果

以上题目主要是注意类,方法的创建和使用 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值