zoukankan      html  css  js  c++  java
  • 练习题2

    父类
    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

  • 相关阅读:
    6 November in 614
    4 November in ss
    标准模板库(STL)
    类模板
    函数模板和模板函数
    关于“宏定义”的作用范围
    运算符重载
    内存分配和释放的函数
    数据库恢复的基础是利用转储的冗余数据
    在局域网络内的某台主机用ping命令测试网络连接时发现网络内部的主机都可以连同,而不能与公网连通,问题可能是
  • 原文地址:https://www.cnblogs.com/1ming/p/5263801.html
Copyright © 2011-2022 走看看