码迷,mamicode.com
首页 > 其他好文 > 详细

练习题2

时间:2016-03-10 23:35:48      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

父类
技术分享
package Zuoye;

public class Car {
    
    
    
    
    private String name;
    private String color;
    private int price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    
    public Car(String name,String color,int price)
    {
        System.out.println("车的构造方法");
        this.name =name;
        this.color=color;
        this.price=price;
    }
    
    public void run()
    {
        System.out.println("我可以跑");
    }
    
    public void xiaohao()
    {
        System.out.println("我能消耗油");
    }
    
    
    
    
    

}
View Code

 

子类

技术分享
package Zuoye;

public class Carddezilei extends Car{

    public Carddezilei()
    {
        super("法拉利", "红色", 1000);
        System.out.println("车的子类的构造方法");
    }
    
    public void run()
    {
        System.out.println("我可以跑的很快");
    }
    
    public void zaizhong()
    {
        System.out.println("我不能载重");
    }
    
    

}
View Code

 

测试类

技术分享
package Zuoye;

public class Cardeceshi {

    public static void main(String[] args) {
        Car c1=new Car("奇瑞", "红色", 1);
        System.out.println("我是"+c1.getName()+",颜色是"+c1.getColor()+",售价是"+c1.getPrice()+"万元");
        c1.run();
        c1.xiaohao();
        System.out.println();
        
        Carddezilei c2=new Carddezilei();
        System.out.println("我是"+c2.getName()+",颜色是"+c2.getColor()+",售价"+c2.getPrice()+"万元");
        c2.run();
        c2.zaizhong();
        c2.xiaohao();
        System.out.println();
        
        Car c3=new Carddezilei();
        System.out.println("我是"+c3.getName()+",颜色是"+c3.getColor()+",售价"+c3.getPrice());
        c3.run();
        c3.xiaohao();
        
    }

}
View Code

技术分享

练习题2

标签:

原文地址:http://www.cnblogs.com/1ming/p/5263801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!