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

Java入门第二季

IMOOC老齐 全栈工程师
难度入门
时长 4小时 0分
  • public class Telphone
    {
        private float screen;
        public float setScreen()
        {    return screen;
        }
        public void setScreen(float screen)
        {    this.screen = screen;
        }
    }


    查看全部
  • 11111111111

    查看全部
  • 7777777777777777

    查看全部
  • 6666666666666   final不会自动初始化

    查看全部
  • 55555555555

    查看全部
  • 44444444444444444

    查看全部
  • 33333333333333

    查看全部
  • 22222222222222

    查看全部
  • 11111111111

    查看全部
  • 定义了成员内部类后,必须使用外部类对象来创建内部类对象,而不能直接去 new 一个内部类对象,即:内部类 对象名 = 外部类对象.new 内部类( );
    查看全部
  • Test.java


    package com.imooc;
    import java.util.*;


    public class Test {
    public static void main(String[] args) {
     Car[] cars = {
       new PassengerCar("奥迪A4",4,0,500),
       new PassengerCar("马自达6",4,0,400),
       new Pickup("皮卡雪6",4,2,450),
       new PassengerCar("金龙",20,0,800),
       new Truck("松花江",0,4,400),
       new Truck("依维柯",0,20,1000)
      };

     // TODO Auto-generated method stub
    System.out.println("********************欢迎使用答答租车系统**************************");
    System.out.println("进入系统请回复:1,退出请回复:2"); 
     Scanner input=new Scanner(System.in);
     int a1=input.nextInt();
     if(a1!=1) {
      System.out.println("********************退出系统*******************************");
      System.exit(0);
     }
     else { 
       System.out.println("有如下车型可供您选择:");
       System.out.println("序号\t\t名称\t\t载客量\t\t载货量\t\t租金");
       for(int i=0;i<cars.length;i++) {
        System.out.println((i+1)+"\t\t"+cars[i]);
       }
       System.out.println("请选择您要租用的车的种类:");
       int a2=input.nextInt();
       //更新车辆数组
       Car[] newCars = new Car[a2];
       int totalprice = 0;
       int totalPerson = 0;
       int totalweight = 0;
       
       int[] nums=new int[2];
       for(int i=0;i<a2;i++) {
        System.out.println("请输入您要租用的第"+(i+1)+"辆车的序号");
        int a3=input.nextInt();
        newCars[i]=cars[a3-1];
        System.out.println("请选择您要租用的数量:");
        int num=input.nextInt();
        nums[i]=num;
        System.out.println("请选择您要租用的天数:");
        int days=input.nextInt();
          totalprice+=newCars[i].getPrice()*days*nums[i];
       }
       System.out.println("-----------------总信息如下-------------------");
              System.out.println("已选载人车:");
              for(int i=0;i<newCars.length;i++)
              {
               if(newCars[i].getPersonnum()!=0) {
                System.out.println(newCars[i].getName());
                totalPerson+=newCars[i].getPersonnum()*nums[i];
               
               }}
               System.out.println("已选载货车:");
           
                for(int i=0;i<newCars.length;i++)
                      {
                  if(newCars[i].getWeight()!=0) {
                    System.out.println(newCars[i].getName());
                    totalweight+=newCars[i].getWeight()*nums[i];
                   
                   }  }
                   System.out.println("总载客数"+totalPerson);
                   System.out.println("总载货数"+totalweight);
                   System.out.println("总租金"+totalprice);
                   System.out.println("*****************谢谢使用**********************");
                 
      }
     }
    }

    Car.java

    package com.imooc;

    public class Car {
     String name;
     int personnum;
     double weight;
     double price;
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public int getPersonnum() {
      return personnum;
     }
     public void setPersonnum(int personnum) {
      this.personnum = personnum;
     }
     public double getWeight() {
      return weight;
     }
     public void setWeight(double weight) {
      this.weight = weight;
     }
     public double getPrice() {
      return price;
     }
     public void setPrice(double price) {
      this.price = price;
     }
    }

    passengerCar.java

    package com.imooc;

    public class PassengerCar extends Car {
     public PassengerCar(String name,int personnum,double weight,double price) {
      this.setName(name);
      this.setPersonnum(personnum);
      this.setWeight(weight);
      this.setPrice(price);
      
     }
     public String toString() {
      return this.getName()+"\t\t"+this.getPersonnum()+"\t\t"+this.getWeight()+"\t\t"+this.getPrice();
     }
     }


    Truck.java

    package com.imooc;

    public class Truck extends Car{
     public Truck(String name,int personnum,double weight,double price) {
      this.setName(name);
      this.setPersonnum(personnum);
      this.setWeight(weight);
      this.setPrice(price);
      
     }
     public String toString() {
      return this.getName()+"\t\t"+this.getPersonnum()+"\t\t"+this.getWeight()+"\t\t"+this.getPrice();
     }
     }

    Pickup.java

    package com.imooc;

    public class Pickup extends Car {
    public Pickup(String name,int personnum,double weight,double price) {
     this.setName(name);
     this.setPersonnum(personnum);
     this.setWeight(weight);
     this.setPrice(price);
     
    }
    public String toString() {
     return this.getName()+"\t\t"+this.getPersonnum()+"\t\t"+this.getWeight()+"\t\t"+this.getPrice();
    }
    }


    查看全部
  • 构造方法必须要与类名一致。

    查看全部
  • Java中获取键盘输入值的三种方法

    方法一:从控制台接收一个字符,然后将其打印出来

    import java.io.*;

    public static void main(String [] args) throws IOException{ 

             System.out.print("Enter a Char:"); 

             char i = (char) System.in.read(); 

             System.out.println("your char is :"+i); 

    虽然此方式实现了从键盘获取输入的字符,但是System.out.read()只能针对一个字符的获取,同时,获取进来的变量的类型只能是char,当我们输入一个数字,希望得到的也是一个整型变量的时候,我们还得修改其中的变量类型,这样就显得比较麻烦。


    方法二:从控制台接收一个字符串,然后将其打印出来。 在这个题目中,我们需要用到BufferedReader类和InputStreamReader类

    import java.io.*;

    public static void main(String [] args) throws IOException{ 

               BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

               String str = null; 

               System.out.println("Enter your value:"); 

               str = br.readLine(); 

               System.out.println("your value is :"+str); 

    }

    这样我们就能获取我们输入的字符串。


    方法三:这种方法我认为是最简单,最强大的,就是用Scanner类

    import java.util.Scanner;

    public static void main(String [] args) { 

             Scanner sc = new Scanner(System.in); 

             System.out.println("请输入你的姓名:"); 

             String name = sc.nextLine(); 

             System.out.println("请输入你的年龄:"); 

             int age = sc.nextInt(); 

             System.out.println("请输入你的工资:"); 

             float salary = sc.nextFloat(); 

             System.out.println("你的信息如下:"); 

             System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary); 

    }

    查看全部
  • 创建对象相当于调用方法,但他有自己的表达式。

    查看全部
  • 父类私有属性,子类不能继承。
    查看全部
    0 采集 收起 来源:Java 中的继承

    2019-01-14

举报

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

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