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

Java入门第二季

IMOOC老齐 全栈工程师
难度入门
时长 4小时 0分
  • 与静态变量一样,我们也可以使用 static 修饰方法,称为静态方法或类方法。其实之前我们一直写的 main 方法就是静态方法。静态方法的使用如:

    运行结果:

    需要注意:

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

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

    2、 在普通成员方法中,则可以直接访问同类的非静态变量和静态变量,如下所示:

    3、 静态方法中不能直接调用非静态方法,需要通过对象来访问非静态方法。如:


    查看全部
  • 外部类名$内部类名.class

    查看全部
  • obj.getClass()  and  obj

    查看全部
  • package Mianji;


    public abstract class Shape {

    private double perimeter;

    private double acreage;

    public double getPerimeter() {

    return perimeter;

    }

    public void setPerimeter(double perimeter) {

    this.perimeter = perimeter;

    }

    public double getAcreage() {

    return acreage;

    }

    public void setAcreage(double acreage) {

    this.acreage = acreage;

    }

    public abstract void Perimeter();

    public abstract void Acreage();


    }

    -------------------------------------------------------------------------------

    package Mianji;

    import java.util.Scanner;

    public class Circl extends Shape {

    Scanner input=new Scanner(System.in);

    {System.out.print("请输入圆的半径:");}

    public double Pi=3.141592653;

    double radius=input.nextDouble();

    @Override

    public void Perimeter() {

    // TODO Auto-generated method stub

    super.setPerimeter(radius*2*Pi);

    System.out.println("圆形的周长是:"+super.getPerimeter());

    }


    @Override

    public void Acreage() {

    // TODO Auto-generated method stub

    super.setAcreage(radius*radius*Pi);

    System.out.println("圆形的面积是:"+super.getAcreage());

    }


    }

    -------------------------------------------------------------------------------

    package Mianji;

    import java.util.Scanner;

    public  class Rectangle extends Shape {

    Scanner input=new Scanner(System.in);

    {System.out.print("请输入矩形的长度:\n");}

    double Long=input.nextDouble();

    {System.out.print("请输入矩形的宽度\n");}

    double Wide=input.nextDouble();

    @Override

    public void Perimeter() {

    // TODO Auto-generated method stub

    super.setPerimeter((Long+Wide)*2);

    System.out.println("矩形的周长为:"+super.getPerimeter());

    }


    @Override

    public void Acreage() {

    // TODO Auto-generated method stub

    super.setAcreage(Long*Wide);

    System.out.println("矩形的面积为:"+super.getAcreage());

    }


    }

    -----------------------------------------------------------------------------

    package Mianji;


    public class Initial {


    public static void main(String[] args) {

    // TODO Auto-generated method stub

    Shape circl=new Circl();

    Rectangle rectangle=new Rectangle();

    circl.Acreage();

    rectangle.Acreage();

    circl.Perimeter();

    rectangle.Perimeter();

    }


    }

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

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

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

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


    查看全部
  • 对象:客观存在的事实
    查看全部
  • Java 中 private default protected public 的范围

    查看全部
  • Java package的名称全部是小写

    查看全部
  • Bus.java

    public class Bus extends Car{

    public Bus() {}

    public Bus(int carId,String carName,int rent,int capacityP,int capacityH){

    this.setCarId(carId);

    this.setCarName(carName);

    this.setRent(rent);

    this.setCapacityP(capacityP);

    this.setCapacityH(capacityH);

    }

    }



    ------------------------------------------


    Pickup.java


    public class Pickup extends Car{

    public Pickup() {};

    public Pickup(int carId,String carName,int rent,int capacityP,int capacityH) {

    this.setCarId(carId);

    this.setCarName(carName);

    this.setRent(rent);

    this.setCapacityP(capacityP);

    this.setCapacityH(capacityH);

    }


    }

    ------------------------------------


    Truck.java


    public class Truck extends Car{

    public Truck() {};

    public Truck(int carId,String carName,int rent,int capacityP,int capacityH){

    this.setCarId(carId);

    this.setCarName(carName);

    this.setRent(rent);

    this.setCapacityP(capacityP);

    this.setCapacityH(capacityH);

    }

    }

    ---------------------------------------

    Car.java

    public abstract class Car {

    private int carId;//车的序列号

    private String carName;//车的名称

    private int rent; //车的租金

    private int capacityP; //载人量

    private int capacityH; //载货量

    public int getCapacityP() {

    return capacityP;

    }

    public void setCapacityP(int capacityP) {

    this.capacityP = capacityP;

    }

    public int getCapacityH() {

    return capacityH;

    }

    public void setCapacityH(int capacityH) {

    this.capacityH = capacityH;

    }

    public int getCarId() {

    return carId;

    }

    public void setCarId(int carId) {

    this.carId = carId;

    }

    public String getCarName() {

    return carName;

    }

    public void setCarName(String carName) {

    this.carName = carName;

    }

    public int getRent() {

    return rent;

    }

    public void setRent(int rent) {

    this.rent = rent;

    }

    }

    --------------------------------

    CarSys.java

    import java.util.Scanner;

    public class CarSys {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

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

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

    Scanner input = new Scanner(System.in);

    int input1 = input.nextInt();

    if(input1!=1) {

    System.out.println("期待您下次光临");

    return;

    }

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

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

    Car[] arrcar= {

    new Bus(1,"奥迪A4",500,4,0),

    new Bus(2,"马自达6",400,4,0),

    new Pickup(3,"皮卡雪6",450,4,2),

    new Bus(4,"金龙",800,20,0),

    new Truck(5,"松花江",400,0,4),

    new Truck(6,"依维柯",1000,0,20)

    };


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

    System.out.println(arrcar[i].getCarId()+"\t"

    +arrcar[i].getCarName()+"\t"

    +arrcar[i].getRent()+"\t"

    +arrcar[i].getCapacityP()+"\t"

    +arrcar[i].getCapacityH());

    }

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

    Scanner input2 = new Scanner(System.in);

    int carNum = input2.nextInt();

    int[] arrinput = new int[carNum];

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

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

    Scanner j = new Scanner(System.in);

    int j1 = j.nextInt();

    arrinput[i]=j1;

    }

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

    Scanner date = new Scanner(System.in);

    int datee = date.nextInt();

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

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

    int temp,count;

    count=0;

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

    for(int j=0;j<arrcar.length;j++) {

    if((arrinput[i]==arrcar[j].getCarId())&&(arrcar[j].getCapacityP()!=0)) {

    System.out.print(arrcar[j].getCarName()+"\t");

    temp = arrcar[j].getCapacityP();

    count = count+temp;

    }

    }

    }

    System.out.println("共载人:"+count+"人");

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

    count=0;

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

    for(int j=0;j<arrcar.length;j++) {

    if((arrinput[i]==arrcar[j].getCarId())&&(arrcar[j].getCapacityH()!=0)) {

    System.out.print(arrcar[j].getCarName()+"\t");

    temp = arrcar[j].getCapacityH();

    count = count+temp;

    }

    }

    }

    System.out.println("共载货:"+count+"吨");

    System.out.print("***租车总价格:");

    count=0;

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

    for(int j=0;j<arrcar.length;j++) {

    if(arrinput[i]==arrcar[j].getCarId()) {

    temp = arrcar[j].getRent();

    count = count+temp;

    }

    }

    }

    System.out.println(count*datee+"元");

    }

    }


    查看全部
  • 特征(属性)=有什么。

    行为(方法)=干什么

    查看全部
  • 程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法

    查看全部
  • public class HelloWorld {

        

        // 定义静态变量score1

        static int score1 = 86;

        // 定义静态变量score2

    static int score2 = 92; 


        // 定义静态方法sum,计算成绩总分,并返回总分

    public static int sum(int a,int b) { 

          int sum=a+b;

          return sum;

    }


    public static void main(String[] args) {

            

            // 调用静态方法sum并接收返回值

    int allScore = HelloWorld.sum(score1,score2);

            

    System.out.println("总分:" + allScore);

    }

    }

    //静态方法之间调用不用实例化对象

    查看全部
  • 一、成员内部类

    1、成员内部类也叫实例内部类。应用场合:每一个外部类对象都需要一个内部类的实例,内部类离不开外部类存在(相当于心脏对人体)

    2、成员内部类的特征:

    作为外部类的一个成员存在,与外部类的属性、方法并列

    成员内部类持有外部类的引用

    成员内部类中不能定义static变量和方法

    3、使用格式:

    Outer outer = new Outer();
    Outer.Inner inner = outer.new Inner();

    二、静态内部类

    1、内部类如果使用static声明,则此内部类就称为静态内部类。(其实也相当于外部类)可以通过外部类 . 内部类来访问。

    2、静态内部类使用场合:内部类不需要外部类的实例(注意区分成员内部类),静态内部类存在仅仅为外部类提供服务或者逻辑上属于外部类,且逻辑上可以单独存在。

    3、静态内部类的特征:

    静态内部类不会持有外部类的引用

    静态内部类可以访问外部的静态变量,如果访问外部类的成员变量必须通过外部类的实例访问

    4、Java中只有内部类才可以是静态的

    使用格式:Outer.Inner inner = new Outer.Inner();

    三、局部内部类:

    1、局部内部类也叫区域内嵌类,局部内部类与成员内部类类似,不过,区域内嵌类是定义在一个方法中的内嵌类。

    2、使用场合:如果内部类对象仅仅为外部类的某个方法使用,使用局部内部类

    3、特征:

    用在方法内部,作用范围仅限于该方法中

    根据情况决定持有外部类对象引用

    不能使用private,protected,public修饰符

    不能包含静态成员

    四、匿名内部类

    1、如果一个内部类在整个操作中只使用一次的话,就可以定义为匿名内部类。匿名内部类也就是没有名字的内部类,这是java为了方便我们编写程序而设计的一个机制,因为有时候有的内部类只需要创建一个它的对象就可以了,以后再不会用到这个类,这时候使用匿名内部类就比较合适。

    2、使用场合:简化内部类的使用

    3、特征:

    使用new创建 ,没有具体位置

    创建的匿名类,默认继承或实现new后面的类型

    根据使用情况决定是否持有外部类对象引用

    内嵌匿名类编译后生成的.class文件的命名方式是”外部类名称$编号.class”,编号为1,2,3…n,编号为x的文件对应的就是第x个匿名类


    查看全部
  • 对字段实现封装的步骤 1. 首先将该字段的访问修饰符改成private 2. 创建该字段对应分
    查看全部
  • 程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法。由于静态初始化块只在类加载时执行一次,所以当再次创建对象 hello2 时并未执行静态初始化块。

    查看全部
  • Car抽象类
    package top.bowen;
    
    public abstract class Car {
       protected int price;//价格
       protected String name;//名称
       public abstract String print();//输出的方法
       public abstract double getPrice();//获取价格
    }
    客车
    package top.bowen;
    
    public class BusCar extends Car {
        protected  int content;//载客量
        public BusCar(String name, int price,int content){
            this.name = name;
            this.price = price;
            this.content = content;
        }
        public String print() {
            return this.name + "\t¥:" + this.price + "元/天\t载人:" + this.content +"人";
        }
    
        public double getPrice() {
            return this.price;
        }
    }
    货车
    package top.bowen;
    
    public class TruckCar extends Car {
        protected  int content;//载货量
        public TruckCar(String name, int price, int content){
            this.name = name;
            this.price = price;
            this.content = content;
        }
        public String print() {
            return this.name + "\t¥:" + this.price + "元/天\t载货:" + this.content + "吨";
        }
    
        public double getPrice() {
            return this.price;
        }
    }
    
    皮卡
    package top.bowen;
    
    public class PiCar extends Car {
        protected  int content;//载货量
        protected  int peopleNum;//载客量
        public PiCar(String name, int price, int content, int peopleNum){
            this.name = name;
            this.price = price;
            this.content = content;
            this.peopleNum = peopleNum;
        }
        public String print() {
            return this.name + "\t¥:" + this.price+ "元/天\t载人:" + this.peopleNum + "人\t载货:" + this.content + "吨";
        }
    
        public double getPrice() {
            return this.price;
        }
    }

    运行结果

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

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

    2019-02-22

举报

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

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