为了账号安全,请及时绑定邮箱和手机立即绑定
  • 定义了成员内部类后,必须使用外部类对象来创建内部类对象,而不能直接去 new 一个内部类对象,即:内部类 对象名 = 外部类对象.new 内部类( );
    查看全部
  • 多态中引用类型转换

    1向上类型转换(隐式/自动类型转换),是小类型到大类型的转换.没有风险.

    2向下类型转换(强制类型转换),是大类型到小类型的转换.有风险,可能会溢出.

    3instanceof运算符,配合条件判断来解决引用对象的类型,避免类型转换的安全性问题.

    查看全部
  • 1班 是 一个类class

    1班的黑板擦,黑板是全局变量

    1班的小明可以看做一个方法

    小明自学了加法

    邻座的小红就绝不知道加法,除非小红也自学加法

    *故,人和人思维不能互通,方法和方法相互独立


    写在1班黑板上的乘法公式

    整个1班的同学(方法)都能够知道(调用)

    但是2班的同学肯定不知道这个公式

    *故,班级和班级之间进度教学不同,类和类相对独立


    如何让2班的知道呢?

    在2班的黑板上写上   1班.乘法公式

    二班的同学就知道,如果需要乘法公式,就去1班黑板上看


    以下代码仅供思维,非严谨可运行结构

    1class{
        int a = 2;
        int b = 3;
        int blackboard_1 = a * b;       //三条都是黑板上的
        xiaoming(){
        int sum = a + b;
        System.out.println(sum);            //我自学加法我知道!
        System.out.println(blackboard_1);   //这题黑板上有,我知道!
        }
        xiaohong(){
        System.out.println(sum);      //嘛玩意?
        System.out.println(blackboard_1); //这题黑板上有,我知道!
        }
    }

    -----------------------班级与班级的分隔墙---------------------

    2class{
        2classall(){
            System.out.println(blackboard_1);    //啥东西?
        }
    }

    在1班创建一个对象    1class blackboard_1 = new 1class

    在2班黑板上写上上条代码,2班的同学就会顺着地址去1班黑板上找。

    *注意,小明觉得电影是[好莱坞],小红觉得布拉德皮特是[好莱坞],虽然都叫好莱坞,但是是不同的东西。局部变量重名不重意。

    *注意,如果黑板上写了小李子=莱昂纳多·迪卡普里奥,小明看了语文书觉得小李子=李白,那么对于小明来说,小李子就是李白,你得到的结果也是李白!所以人都是自私以自己为中心存在的混蛋。

    *默认值什么的反之会报错,不用管哈哈

    *按道理小明自学的东西肯定不能传给别的班级的人,当然聪明的秃头侠肯定有方法,本次不讲。

    查看全部
  • // 父类
    package exercisePolymorphisn;
    public class Vehicle {	
        private String name;	private int capacity;		
      public Vehicle(String name, int capacity) {		
          this.name = name;		
        this.capacity = capacity;		
        System.out.println("交通工具");	}	
        
        // getter & setter    
        public String getName() {	return name;	}	
        public void setName(String name) {		this.name = name;	}	
        public int getCapacity() {		return capacity;	}	
        public void setCapacity(int capacity) {		this.capacity = capacity;	}	
            
        // toString() 方法    
        @Override	
        public String toString() {		
           return "Vehicle [name=" + name + ", capacity=" + capacity + "]";
        }
     }
     
     // 公共汽车
     package exercisePolymorphisn;
     public class AutoBus extends Vehicle{		
         public AutoBus() {    
             super("公交车",40);
         	}
     }
     
     // 轮船
     package exercisePolymorphisn;
     public class Ship extends Vehicle{
     	public Ship() {		
     	    super("轮船",200);
     	 }
     }
     
     // 飞机
     package exercisePolymorphisn;
     public class Airplane extends Vehicle{		
         public Airplane() {
             super("飞机",100);	
          }
     }
     
     // Main 方法
     package exercisePolymorphisn;
     public class Main {	
         public static void main(String[] args) {				
             Vehicle v = new Airplane();		
             System.out.println(v);				
             
             v = new AutoBus();		
             System.out.println(v);
          }
     }


    查看全部
    3 采集 收起 来源:Java 中的多态

    2019-09-19

  • equals()方法比较的是对象的引用是否指向同一个地址,在类名 对象名=new 类名()时,每new一次会开辟一个新的内存地址,每次的并不相同
    equals语句返回值是布尔型,通常与if配合使用
    【当new一个类的时候得到的对象是类的对象,如果这个对象调用getClass()方法得到的是类对象//类对象关注的是类的代码信息,即类的属性名,方法名等,类的对象关注的是类的数据信息,即具体值】
    如果要用变量名即对象名来判断两个对象名中的属性值是否相等时,则要改写equals()方法,【通过resource中查找】【引用格式 对象名.equals(另一个对象名)】使其达到目标

     

    在eclipse用Source选项中的功能可以快速生成重写equals方法


    查看全部
  • 接口:

    1. 概念:类是一种具体实现的体,而接口定义了某一批所需要遵守的规范,接口不关心这些类的内部数据,也不关心这些类里的方法实现细节,他只规定这些类里必须提供某些方法。

    2. 定义:不用class用interface关键字

    3. 接口定义的基本语法:

      [修饰符] abstract interface 接口名[extends 父接口1,父接口2...]

      {

      零到多个常量定义...

      零到多个抽象方法定义...

      }

    ⚠️:接口就是用来被继承,被实现的,修饰符一般建议用public 不能使用private/protected修饰接口

    接口定义:

        常量:接口中的属性是常量,即使定义是不添加,public static final  修饰符,系统也会自动加上。

        方法:接口中的方法只能是抽象方法,总是使用,即使定义是不添加 public abstract 修饰符,系统也会自动加上。

        使用接口:一个类实现一个或多个接口,实现接口使用implements关键字。Java中一个类只能继承一个父类,不够灵活,通过实现多个接口可以做补充。

    继承父类实现接口的语法为:

    [修饰符] class 类名 extends 父类 implements 接口1,接口2...

    {

    类体部分//如果继承了抽象类,需要实现继承的抽象方法;要实现接口中的抽象方法。

    }

    //如要继承父类,继承父类需在实现接口之前

    接口的使用:在接口的使用当中,经常与匿名内部类配合使用

    https://img1.sycdn.imooc.com//5cee54e40001b6aa14480456.jpg


    查看全部
    3 采集 收起 来源:Java 中的接口

    2019-05-31

    1. 对象:

      对特定一个存在事物       客观存在的事物         万物皆对象

    2. 面向对象:

      以自我为中心对待一个特定事物存在,需要自己描述出来的符合的条件              描述的买电话的过程就是面向对象https://img1.sycdn.imooc.com//5cabe32f0001bf3608350613.jpg

    3. 类:

      是模子,是你限制你需要的东西的条件

      具有相同属性和方法的一组对象的集合

      类型,对象的类型

      用来描述对象的特征(属性)和行为(方法)

    4. 类与对象的关系/区别:

      类是抽象的概念是一个模板,是条件信息。

      对象是一个具体的东西,是一个客观存在。

      类是抽象的概念,模板。描述具体的对象信息。对象是具体实体,客观存在。

      好比是类是能打电话,能发短信的功能条件。而对象就是一部手机

    5. 对象的属性:

      对象拥有的各种特征(有什么),他具有什么就是对象的属性。

      例如手机的屏幕,cpu,内存等等。

    6. 对象的方法:

      对象执行的操作(能做什么)




    查看全部
  • 类的实例化是对象,new helloworld()就是把hello world这个类实例化

    hello=new helloworld()是将其实例化后赋给一个变量,然后引用变量就可以访问这个类中内容 

    类是以代码形式保存在文件中,用这个类时需要把他加载到内存中,这个过程叫做类的实例化,实例化后便于操作会把它赋值给一个变量,通过操作变量来进行对类中属性和方法的操作,类是对象的类型,所以前边用类名修饰

    变量名.属性//变量名.方法()//就可以调用类中内容

    查看全部
  • 答答租车程序

    <Car类>

    package com.imooc;
    public class Car {
        //车辆编号
        private int number;
        //车辆名称
        private String name;
        //车辆租金
        private float price;
        
        public int getNumber() {
            return number;
        }
        public void setNumber(int number) {
            this.number = number;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public float getPrice() {
            return price;
        }
        public void setPrice(float price) {
            this.price = price;
        }
        @Override
        public String toString() {
            return "序号:" + number + '\t'+ "名称:" + name + '\t' + "价格:" + price + '\t';
        }
    }



    <PassengerCar类>

    package com.imooc;
    public class PassengerCar extends Car{
        //载客量
        private int passenger;
        public int getPassenger() {
            return passenger;
        }
        public void setPassenger(int passenger) {
            this.passenger = passenger;
        }
        
        public PassengerCar(int newNumber,String newName,float newPrice,int passenger) {
            super.setName(newName);
            super.setNumber(newNumber);
            super.setPrice(newPrice);
            this.setPassenger(passenger);
        }
        @Override
        public String toString() {
            return super.toString()+"载客量:" + passenger;
        }
    }



    <TrunkCar类>

    package com.imooc;
    public class TruckCar extends Car{
        //载货量
        private double capacity;
        public double getCapacity() {
            return capacity;
        }
        public void setCapacity(double capacity) {
            this.capacity = capacity;
        }
        public TruckCar(int newNumber,String newName,float newPrice,double capacity) {
            super.setName(newName);
            super.setNumber(newNumber);
            super.setPrice(newPrice);
            this.setCapacity(capacity);
        }
        @Override
        public String toString() {
            return super.toString()+"载货量:" + capacity;
        }
    }



    <PickCar类>

    package com.imooc;
    public class PickCar extends Car {
        //载货量
        private double capacity;
        public double getCapacity() {
            return capacity;
        }
        public void setCapacity(double capacity) {
            this.capacity = capacity;
        }
        //载客量
        private int passenger;
        public int getPassenger() {
            return passenger;
        }
        public void setPassenger(int passenger) {
            this.passenger = passenger;
        }
        
        public PickCar(int newNumber,String newName,float newPrice,int passenger,double capacity) {
            super.setName(newName);
            super.setNumber(newNumber);
            super.setPrice(newPrice);
            this.setCapacity(capacity);
            this.setPassenger(passenger);
        }
        @Override
        public String toString() {
            return super.toString()+"载货量:" + capacity + '\t' + "载客量:" + passenger;
        }
    }



    <Initial类>

    package com.imooc;
    import java.util.Scanner;
    public class Initial {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //创建汽车对象
            Car[] cars = new Car[5];
            cars[0] = new TruckCar(1,"重型卡车 ",460,4.5);
            cars[1] = new TruckCar(2,"轻型卡车 ",300,3.5);
            cars[2] = new PassengerCar(3,"大客车",500,45);
            cars[3] = new PassengerCar(4,"小客车",400,30);
            cars[4] = new PickCar(5,"皮卡",450,6,3.5);
            
            System.out.println("欢迎使用答答租车系统!");
            System.out.println("请问您是否需要租车?" + '\t' + "1.是" + '\t' + "2.否");
            Scanner s = new Scanner(System.in);
            int in = s.nextInt();
            //判断是否租车
            if(in == 1) {
                //输出汽车列表
                System.out.println("-------------------------------");
                System.out.println("您可租车的类型及其价目表:");
                for(Car c:cars) {
                    System.out.println(c.toString());
                }
                
                //客户选择车辆数目
                System.out.println("-------------------------------");
                System.out.println("请选择您租赁汽车的数量:");
                int num = s.nextInt();
                //客户选择车辆型号
                Car[] rentCars = new Car[num];
                for(int i = 1; i <= num ;i++) {
                    System.out.println("请选择您第" + i + "辆车型号(请输入车辆序号):");
                    int j = s.nextInt();
                    rentCars[i-1] = cars[j-1];
                }
                //客户选择租赁时间
                System.out.println("请输出您租赁时间:");
                int time = s.nextInt();
                
                //计算金额
                double totalprice = 0;
                for(int i = 0; i < num; i++) {
                    totalprice += rentCars[i].getPrice()*time;
                }
                //计算总载货量、总载客量
                double totalcapacity = 0;
                int totalpassenger = 0;
                for(int i = 0; i < num; i++) {
                    if(rentCars[i] instanceof PassengerCar) {
                        totalpassenger += ((PassengerCar)rentCars[i]).getPassenger();
                    }
                    if(rentCars[i] instanceof TruckCar) {
                        totalcapacity += ((TruckCar)rentCars[i]).getCapacity();
                    }
                    if(rentCars[i] instanceof PickCar) {
                        totalpassenger += ((PickCar)rentCars[i]).getPassenger();
                        totalcapacity += ((PickCar)rentCars[i]).getCapacity();
                    }
                }
                
                //输出租赁信息
                System.out.println("-------------------------------");
                System.out.println("租赁车辆列表如下:");
                for(Car c1:rentCars) {
                    System.out.println(c1.toString());
                }
                System.out.println("总载客量为:" + totalpassenger);
                System.out.println("总载货量为:" + totalcapacity);
                System.out.println("租赁时间为:" + time + "天");
                System.out.println("您共需支付:" + totalprice);
            }
        }
    }

    查看全部
  • final修饰方法该方法不允许被覆盖,重写(子类中) ~修饰类则该类不可以被继承 ~修饰变量则变量需声明时赋值且不可更改。

    查看全部
    3 采集 收起 来源:练习题

    2019-01-07

  • package actual_combat;
    
    public abstract class Auto {
        private int num;//序号
        private String name;//车名
        private int price;//租价
        private int carload;//载人数
        private int cargoload;//载货量
    
        public int Number(){
            return num;
        }
        public void Number1(int num) {
            this.num=num;
        }
        public String Name(){
            return name;
        }
        public  void Name1(String name){
            this.name=name;
        }
        public int Price(){
            return price;
        }
        public void Price1(int price){
            this.price=price;
        }
        public int Carload(){
            return carload;
        }
        public void Carload1(int carload){
            this.carload=carload;
        }
        public int Cargoload(){
            return cargoload;
        }
        public void Cargoload1(int cargoload){
            this.cargoload=cargoload;
        }
    
        @Override
        public String toString() {
            return num+"  "+name+"\t"+price+"    "+carload+"       "+cargoload;
        }
    }
    package actual_combat;
    
    public class Bus extends Auto {
        public Bus(int num,String name,int price,int carload){
            this.Number1(num);
            this.Name1(name);
            this.Price1(price);
            this.Carload1(carload);
        }
    }
    package actual_combat;
    
    public class Car extends Auto {
        public Car(int num,String name,int price,int carload){
            this.Number1(num);
            this.Name1(name);
            this.Price1(price);
            this.Carload1(carload);
        }
    }
    package actual_combat;
    
    public class Pickup extends Auto{
        public Pickup(int num,String name,int price,int carload,int cargoload){
            this.Number1(num);
            this.Name1(name);
            this.Price1(price);
            this.Carload1(carload);
            this.Cargoload1(cargoload);
        }
    }
    package actual_combat;
    
    public class Truck extends Auto{
        public Truck(int num,String name,int price,int cargoload){
            this.Number1(num);
            this.Name1(name);
            this.Price1(price);
            this.Cargoload1(cargoload);
        }
    }
    package actual_combat;
    import java.util.Scanner;
    
    public class Runtest {
        public void runtest() {
            Auto[] cars = new Auto[6];
            cars[0] = new Car(1, "奥迪A4", 500, 4);
            cars[1] = new Car(2, "马自达6", 400, 4);
            cars[2] = new Pickup(3, "丰田皮卡", 450, 4, 2);
            cars[3] = new Bus(4, "金龙", 800, 20);
            cars[4] = new Truck(5, "松花江", 400, 4);
            cars[5] = new Truck(6, "依维柯", 1000, 20);
            System.out.println("您可租车的类型即价格表!");
            System.out.println("序号 车名  租价  载客人数  载货量");
            for (int i = 0; i < cars.length; i++) {
                System.out.println(cars[i]);
            }
            System.out.println("请输入租用汽车的数量!");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();//输入选项
            while (a <= 0) {
                System.out.println("WARING!输入车辆数量有误!!!");
                a = scanner.nextInt();//输入选项
            }
                int endPrice = 0;//总价格
                int allpeople = 0;//总人数
                int allthing = 0;//总货物数
                String[] NAME = new String[a];//数量为a的所有车型名字
                for (int i = 0; i < a; i++) {
                    System.out.println("请输入第" + (i + 1) + "量车序号");
                        int b = scanner.nextInt();//输入选项
                        NAME[i] = cars[b - 1].Name();//第b辆汽车的名字
                        endPrice += cars[b - 1].Price();//第b辆汽车的价格
                        allpeople += cars[b - 1].Carload();//第b辆汽车的载人数
                        allthing += cars[b - 1].Cargoload();//第b辆汽车的载货数
                }
                System.out.println("请输入需要租用的天数!");
                int c = scanner.nextInt();
                System.out.println("确认本次订单!");
                System.out.println("本次一共使用了:");
                for (int i = 0; i < a; i++) {
                    System.out.print(NAME[i]);
                    System.out.println("!");
                }
                System.out.println("本次一共租用:" + a + "  辆汽车!可载人数为:" + allpeople + "!可载物:" + allthing + "吨");
                System.out.println("本次租车一共花费" + endPrice * c + "元!");
        }
    }
    package actual_combat;
    
    import java.util.Scanner;
    
    public class test {
        public static void main(String[] args) {
            System.out.println("欢迎来到租车系统!继续请按 1 ;退出请按 0 !");
            Scanner scanner = new Scanner(System.in);
            int a = scanner.nextInt();//输入选项
            while (a != 1 && a != 0) {
                System.out.println("请重新输入 选项 !");
                a = scanner.nextInt();
            }
            if (a == 1) {
             new Runtest().runtest();
            }
        }
    }


    查看全部
    4 采集 收起 来源:综合练习

    2018-12-08

  • package dadadache;
    /**
     * 车的父类
     */
    public abstract class Car {
    protected int id;// 车的id
    protected String name;// 车的名字
    protected double price;// 车的价格
    public Car(int id, String name, double price) {
    setId(id);
    setName(name);
    setPrice(price);
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public double getPrice() {
    return price;
    }
    public void setPrice(double price) {
    this.price = price;
    }
    @Override
    public String toString() {
    return id + ".\t" + name + "\t" + price + "元/天" + "\t";
    }
    }
    package dadadache;
    /**
     * 客车
     */
    public class Bus extends Car {
    private int capacity;// 载人
    public Bus(int id, String name, double price, int capacity) {
    super(id, name, price);
    setCapacity(capacity);
    }
    public int getCapacity() {
    return capacity;
    }
    public void setCapacity(int capacity) {
    this.capacity = capacity;
    }
    @Override
    public String toString() {
    return super.toString() + "载人:" + capacity + "人";
    }
    }
    package dadadache;
    /**
     * 货车
     */
    public class Truck extends Car {
    private double load; //载物
    public Truck(int id, String name, double price, double load) {
    super(id, name, price);
    setLoad(load);
    }
    public double getLoad() {
    return load;
    }
    public void setLoad(double load) {
    this.load = load;
    }
    @Override
    public String toString() {
    return super.toString() + "载货:" + load + "吨";
    }
    }
    package dadadache;
    /**
     * 皮卡
     */
    public class PickUp extends Car {
    private int capacity;// 载人
    private double load; //载物
    public PickUp(int id, String name, double price, int capacity, double load) {
    super(id, name, price);
    setCapacity(capacity);
    setLoad(load);
    }
    public int getCapacity() {
    return capacity;
    }
    public void setCapacity(int capacity) {
    this.capacity = capacity;
    }
    public double getLoad() {
    return load;
    }
    public void setLoad(double load) {
    this.load = load;
    }
    @Override
    public String toString() {
    return super.toString() + "载人:" + capacity + "人" + " 载货:" + load + "吨";
    }
    }
    package dadadache;
    import java.util.Scanner;
    public class Driver {
    private void printAllCars(Car[] cars) {
    for (Car car: cars) {
    System.out.println(car);
    }
    }
    public static void main(String[] args) {
    System.out.println("欢迎使用哒哒租车系统:");
            System.out.print("您是否要租车:1.是 0.否");
            
            Car[] cars = {new Bus(1, "奥迪A4", 400, 4), new Bus(2, "马自达6", 400, 4),
            new PickUp(3, "皮卡雪6", 450, 4, 2), new Bus(4, "金龙", 800, 20),
            new Truck(5, "松花江", 400, 4), new Truck(4, "依维柯", 1000, 20)};
            
            Scanner scan = new Scanner(System.in);
            int option = scan.nextInt();//获取控制台上用户输入的数字
            
            if (option == 1) {
            Driver driver = new Driver();
           
            System.out.println("您可租车的类型及价目表:");
                System.out.println("序号\t汽车名称\t租金\t\t容量");
                driver.printAllCars(cars);
                
                System.out.print("请输入您要租汽车的数量:");
                int num = scan.nextInt();
                Car[] rentCars = new Car[num];
                int count = 0, totalCapacity = 0; // 车的总数以及总载人量
                double totalLoad = 0, totalPrice = 0; // 总载物量以及总价
                String bus = " ";
                String truck = " ";
                
                outer:
                while(count < rentCars.length) {
                System.out.print("请选择第" + (count + 1) + "辆车的序号");
                int index = scan.nextInt();
                // 检查序号是否正确,如果不正确继续循环
                if (index < 1 || index > cars.length) {
                System.out.print("请输入正确的序号");
                continue outer;
                }
                // 将想租的车的信息放到租车数组,增加count,更新载人/载物信息和价格
                Car car = cars[index-1];
                rentCars[count] = car;
               
                if (car instanceof Bus) {
                totalCapacity += ((Bus) car).getCapacity();
                bus += car.getName();
                } else if (car instanceof Truck) {
                totalLoad += ((Truck) car).getLoad();
                truck += car.getName();
                } else { // 如果是皮卡
                totalCapacity += ((PickUp) car).getCapacity();
                totalLoad += ((PickUp) car).getLoad();
                bus += car.getName();
                truck += car.getName();
                }
                totalPrice += car.getPrice();
               
                count++;
    }
                
                System.out.print("请输入租的天数:");
                int days = scan.nextInt();
                totalPrice *= days;
                
                System.out.println("您的账单:");
                System.out.println("***可载人的车有:\n" + bus + "\t共载人" + totalCapacity + "人\n" 
                + "***可载货的车有:\n" + truck + "\t共载货" + totalLoad + "吨\n"
                + "***租车总价格金额:" + totalPrice + "元");
    //            driver.printAllCars(rentCars);
            }
            
            scan.close();
    }
    }


    查看全部
    3 采集 收起 来源:综合练习

    2018-11-13

  • import java.util.Scanner;


    public class Initil {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    Vehicle[] vehicles={new Car("奥迪A4",4,0,500),new Car("马自达6",4,0,400),new Truck("皮卡雪6",4,2,450),new Car("金龙",20,0,800),

    new FreightCar("松花江",0,4,400),new FreightCar("依维柯",0,20,1000)};

    System.out.println("欢迎来到答答租车系统!");

    System.out.println("是否进行租车  (1 是  2 不是)");

    Scanner in =new Scanner(System.in);

    int a=in.nextInt();

    if(a==1){

    System.out.println("您可租车的类型和价目表:");

    System.out.println("序号\t汽车名称\t租金\t  载人\t载货");

    for(int i=0;i<vehicles.length;i++) {

    System.out.println((i+1)+"\t"+vehicles[i]);

    }

    System.out.println("请输入您要租汽车的数量:");

    int num=in.nextInt();

    Vehicle[] chooseCar=new Vehicle[num];

    int serial;

    for(int i=0;num>0;num--,i++) {

    System.out.println("请输入第"+(i+1)+"辆车的序号:");

    serial=in.nextInt();

    chooseCar[i]=vehicles[serial-1];

    }

    System.out.println("根据您的输入,您想要租的车为:");

    for(int i=0;i<chooseCar.length;i++) {

    System.out.println((i+1)+"\t"+chooseCar[i]);

    }

    System.out.println("请输入租车天数:");

    int days=in.nextInt();

    System.out.println("可载人的有:");

    for(int i=0;i<chooseCar.length;i++){

    if(chooseCar[i].getLoadperson()>0){

    System.out.println(chooseCar[i].name);

    }

    }

    System.out.println("可载货的有:");

    for(int i=0;i<chooseCar.length;i++){

    if(chooseCar[i].getLoadthings()>0){

    System.out.println(chooseCar[i].name);

    }

    }

    int totalCost=0;

    for(int i=0;i<chooseCar.length;i++){

    totalCost+=chooseCar[i].getRent();

    }

    System.out.println("租车总价格为:"+totalCost*days);

    }

    in.close();

    }

    }


    public class Vehicle {

    public String name;

    public int loadperson;

    public int loadthings;

    public int rent;

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getLoadperson() {

    return loadperson;

    }

    public void setLoadperson(int loadperson) {

    this.loadperson = loadperson;

    }

    public int getLoadthings() {

    return loadthings;

    }

    public void setLoadthings(int loadthings) {

    this.loadthings = loadthings;

    }

    public int getRent() {

    return rent;

    }

    public void setRent(int rent) {

    this.rent = rent;

    }

    }


    public class Car extends Vehicle {

    public Car(String name,int loadperson,int loadthings,int rent){

    this.name=name;

    this.loadperson=loadperson;

    this.loadthings=loadthings;

    this.rent=rent;

    }


    @Override

    public String toString() {

    return this.getName()+"\t"+this.getRent()+"元/天    载人:"+this.getLoadperson()+"\t载货:"+this.getLoadthings()+"t";

    }

    }


    查看全部
  • 1.包的作用:    管理java文件    解决同名文件冲突 2.定义包:package包名    注:必须放在java源程序的第一行           包名间可以使用"."号隔开           eg:com.imooc.MyClass 3.系统中的包   java.(功能).(类)   java.lang.(类)包含java语言基础的类   java.util.(类)包含java语言中各类工具类   java.io.(类)包含输入、输出相关功能的类 4.包的使用 1)可以使用import关键字,在某个文件使用其他文件中的类。       import com.imooc.music.MyClass 2)java中,包的命名规范是全小写字母拼写 3)使用的时候不但可以加载某个包下的所有文件     eg:com.imooc.*     也可以加载某个具体子包下的所有文件     eg:com.imooc.music.*

    查看全部
  • 类名首字母大写

    https://img1.sycdn.imooc.com//5b14836600014eb714451001.jpg

    https://img1.sycdn.imooc.com//5b14838a000186e614451001.jpg

    查看全部

举报

0/150
提交
取消
课程须知
本课程是Java开发的基础,需要大家:掌握 Java 基本语法的使用。如果您是新手,建议先移步 《Java入门第一季》https://www.imooc.com/learn/85
老师告诉你能学到什么?
• 掌握 Java 编程思路 • 熟练运用面向对象程序设计思想

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!