为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Java入门第二季 升级版_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第二季 升级版

IMOOC老齐 全栈工程师
难度入门
时长 4小时 0分
  • 静态成员可以使用类名直接访问,也可以使用对象名进行访问。

    查看全部
  • package com.cuit;

    import java.util.Scanner;

    public class Initail {


    /**

    * @param args

    */

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    System.out.println("欢迎使用哒哒租车系统");

    System.out.println("您是否要租车:1是 0否");

    Car[] allcar = {new PassengerCar("奥迪A4", 800, 4), new PassengerCar("大金龙", 500, 20), 

    new Trunk("东风", 800, 20), new Trunk("解放",500, 10), new Pickup("皮卡", 800, 4, 5)};

    Scanner input = new Scanner(System.in);

    int choice1 = input.nextInt();

    if(choice1 == 1){

    System.out.println("您可租的汽车及其价目表:");

    System.out.println("序号\t汽车名称\t租金\t\t容量");

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

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

    }

    }else if(choice1 != 1){

    System.out.println("感谢你的使用");

    }

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

    int choice2 = input.nextInt();

    if(choice2 < 1){

    System.out.println("数量错误,请重新输入");

    choice2 = input.nextInt();

    }

    //更新车辆数组

    Car[] newcar = new Car[choice2];

    double bills = 0;

    double zhuo = 0;

    int zren = 0;

    for(int i = 0; i < choice2; i++){

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

    int choice3 = input.nextInt();

    if(choice3 > 5 || choice3 < 1){

    System.out.println("目标不存在,请重新输入");

    choice3 = input.nextInt();

    }

    newcar[i] = allcar[choice3 - 1];

    }

    System.out.println("请输入你要租的天数");

    int day = input.nextInt();

    //计算租金

    for(int i = 0; i < choice2; i++){

    bills = newcar[i].getRent() * day + bills;

    }

    //输出账单

    System.out.println("您的账单信息:");

    //可载人的车

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

    for(int i = 0; i < choice2; i++){

    if(newcar[i].getManned() != 0){

    System.out.println(newcar[i]);

    zren = zren + newcar[i].getManned();

    }

    }

    //可以载货的车

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

    for(int i = 0; i < choice2; i++){

    if(newcar[i].getCargo() != 0 ){

    System.out.println(newcar[i]);

    zhuo += newcar[i].getCargo();

    }

    }

    //输出账单总金额

    System.out.println("总金额:"+ bills);

    System.out.println("总载客人数:" + zren + "人");

    System.out.println("总载货数:" + zhuo + "吨");



    }


    }







    package com.cuit;


    public abstract class Car { //利用封装概念设置属性

    private String name;

    private double rent;

    private int manned;//定义载客数

    private double cargo;//定义载货数

    /**

    * @return the name

    */

    public String getName() {

    return name;

    }

    /**

    * @param name the name to set

    */

    public void setName(String name) {

    this.name = name;

    }

    /**

    * @return the rent

    */

    public double getRent() {

    return rent;

    }

    /**

    * @param rent the rent to set

    */

    public void setRent(double rent) {

    this.rent = rent;

    }

    /**

    * @return the manned

    */

    public int getManned() {

    return manned;

    }

    /**

    * @param manned the manned to set

    */

    public void setManned(int manned) {

    this.manned = manned;

    }

    /**

    * @return the cargo

    */

    public double getCargo() {

    return cargo;

    }

    /**

    * @param cargo the cargo to set

    */

    public void setCargo(double cargo) {

    this.cargo = cargo;

    }

    //使用get set进行读写属性


    }



    package com.cuit;


    public class PassengerCar extends Car {//继承Car

    public PassengerCar(String name, double rent, int manned){

    this.setName(name);

    this.setRent(rent);

    this.setManned(manned);

    }

    public String toString(){

    return this.getName() + "\t" + this.getRent() + "/天" +"\t" + "可乘坐" + this.getManned() + "人";

    }


    }












    package com.cuit;


    public class Pickup extends Car {

    public Pickup(String name, int manned, double rent, double cargo){

    this.setCargo(cargo);

    this.setManned(manned);

    this.setName(name);

    this.setRent(rent);

    }

    public String toString(){

    return this.getName() + "\t" + this.getRent() + "/天" + "\t" + "可乘坐:" + this.getManned() + "人" + "可载货:" + this.getCargo() + "吨";

    }


    }







    package com.cuit;


    public class Trunk extends Car {

    public Trunk(String name, double rent, double cargo){

    this.setCargo(cargo);

    this.setRent(rent);

    this.setName(name);

    }

    public String toString(){

    return this.getName() + "\t" + this.getRent() + "/天" + "\t" + "可载货:" + this.getCargo() + "吨";

    }



    }


    查看全部
  • 接口中方法不能有方法体,同时方法的访问修饰符不能是 private 和 protected

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

    2019-03-17

  • 抽象类使用规则:a.abstract定义抽象类,定义抽象方法,只有声明,不需实现。 抽象方法没有方法体,以 ;号结束 public abstract void call();


    查看全部
  • 	HelloWorld hello = new HelloWorld(); 
    	       // 创建内部类对象
    	       Inner i = hello.new Inner();

    创建内部类需要首先创建外部类

    查看全部
  • https://img1.sycdn.imooc.com//5c8dd96700018ec609800318.jpg

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

    我们创建对象时,其实是执行构造方法

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

    https://img1.sycdn.imooc.com//5c8ddd8f000188fb05830157.jpg//构造方法可以保证给对象的属性赋一个合理的值

    查看全部
  • 构造方法的语句格式:

    public 构造方法名()

    {

    //初始化代码

    }

    //其中构造方法名与类名相同!

    //语句格式中没有返回值类型!

    //没有参数的构造方法叫做无参构造方法!有指定参数的构造方法为有参构造方法!

    //有参构造方法只有一个作用:给成员变量赋予初值!

    //当没有指定构造方法时,系统会自动添加无参的构造方法!

    //当有指定构造方法,无论是有参、无参的构造方法,都不会自动添加无参的构造方法!

    //构造方法的重载:方法名相同,但参数不同的多个方法,调用时会自动根据不同的参数选择相应的方法!

    查看全部
  • 成员变量和局部变量的区别:

    1、作用域不同:

    局部变量的作用域仅限于定义它的方法;

    成员变量的作用域在整个类内部都是可见的;

    2、初始值不同:

    java会给成员变量一个初始值,而不会给局部变量赋予初始值!

    3、在同一个方法中,不允许有同名局部变量;而在不同方法中,可以有同名局部变量。

    4、两类变量同名时,局部变量具有更高的优先级!


    查看全部
  • 1、创建对象:

    类名 对象名 = new 类名();

    2、使用对象:

    引用对象的属性:对象名.属性;

    引用对象的方法:对象名.方法();

    查看全部
  • 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问 

    查看全部
  • 1、 静态方法中可以直接调用同类中的静态成员,但不能直接调用非静态成员。如:

    如果希望在静态方法中调用非静态变量,可以通过创建类的对象,然后通过对象来访问非静态变量。如:


    查看全部
  • Java不会给局部变量赋予初始值

    查看全部
  • 类 class 

     属性(成员变量)有什么

     方法   干什么

    查看全部
  • 接口中方法不能有方法体

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

    2019-03-16

  • ,程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法。由于静态初始化块只在类加载时执行一次,所以当再次创建对象 hello2 时并未执行静态初始化块。

    查看全部

举报

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

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