构造器(有参、无参)

构造器: 就是和类名相同但无返回类型的方法。用于当前或某一对象的实例化,并将当前或某一对象返回。无参构造:1、如果使用new关键字实际上是调用无参构造器;2、无参构造往往是为了初始化一些值。有参构造:一旦定义了有参构造,必须要显示定义无参构造
使用构造器需注意:

1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名)

2.每个类可以有一个以上的构造器

3.构造器可以有0个、1个或1个以上的参数

4.构造器没有返回值

5.构造器总是伴随着new操作一起调用

下面是有参无参构造的一个例子:

1、编写一个交通工具类Vehicle类。具有:属性——载客量(capacity),方法:(1)无参构造方法(给capacity初始化值为2,并输出“执行交通工具类的无参构造方法。”); (2)有参构造方法(传参给capacity初始化,并输出“执行交通工具的有参构造方法。”);(3) capacity的set、get方法;(4) print方法:输出capacity. (5)run方法,从控制台中输出“这是交通工具run方法”。
2、创建Vehicle类的三个子类,Motor类表示汽车,Ship类表示船,Aeroplane类表示飞机类.各自具有;属性——speed,表示速度. 方法: (1)无参构造方法(给speed初始化值为0,并输出“执行XX类的无参构造方法。”);(2)有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出“执行XX类的有参构造方法。”)(3) 加速(speedup):speed+10并返回speed;(4) 减速(speeddown):speed-15并返回speed;(5) 重写print方法:输出speed和capacity。(6) 三个类各自的run方法.分别输出”汽车正在公路上奔跑”,”轮船正在大海上航行”,”飞机在天上飞行”
3、创建Motor的二个子类, Bus和Car(均为final类),分别表示公共汽车和轿车,分别写出各自的run方法。各自具有属性——载客量(capacity);方法:(1) 无参构造方法(给capacity初始化值为20,并输出“执行XX类的无参构造方法。”)(2) 有参构造方法(用super关键字调用父类的有参构造方法,传参给capacity初始化,并输出“执行XX类的有参构造方法。”)(3)写出它们自己的run方法.
同理,写出轮船的两个子类,航空母舰和豪华游轮.飞机的两个子类,战斗机和客机.
4、main函数中调用无参构造方法创建一个Car的对象car;调用加速方法将速度加至50,调用print方法和run方法;调用减速方法,将速度减至20,调用print方法。调用有参构造方法创建一个Bus的对象bus;调用print方法和run方法。航空母舰和豪华游轮,战斗机和客机.分别选择一个构造方法,并调用print和run方法.

package 交通工具;

public class Main {
    public static void main(String[] args) {
        System.out.println("汽车类->");
        Car car = new Car();
        car.setSpeed(40);
        System.out.println("最开始的初速度为:40");
        System.out.println("加速***********");
        car.speedUp();
        car.print();
        car.run();
        System.out.println("减速***********");
        car.speedDown();
        car.speedDown();
        car.print();
        System.out.println("****************");
        Bus bus = new Bus(10,100);
        bus.print();
        bus.run();
        System.out.println("船的子类->");
        Birdfarm birdfarm = new Birdfarm();
        birdfarm.setSpeed(40);
        System.out.println("最开始的初速度为:40");
        System.out.println("加速***********");
        birdfarm.speedUp();
        birdfarm.print();
        birdfarm.run();
        System.out.println("减速***********");
        birdfarm.speedDown();
        birdfarm.speedDown();
        birdfarm.print();
        System.out.println("****************");
        Luxuryship luxuryship = new Luxuryship(10,100);
        luxuryship.print();
        luxuryship.run();
        System.out.println("飞机的子类->");
        Fighter fighter = new Fighter();
        fighter.setSpeed(40);
        System.out.println("最开始的初速度为:40");
        System.out.println("加速***********");
        fighter.speedUp();
        fighter.print();
        fighter.run();
        System.out.println("减速***********");
        fighter.speedDown();
        fighter.speedDown();
        fighter.print();
        System.out.println("****************");
        Aircraft aircraft = new Aircraft(10,100);
        aircraft.print();
        aircraft.run();


    }
    
}

交通工具

package 交通工具;

public class Vehicle {
    public int capacity;
    Vehicle() {
        capacity=2;
        System.out.println("执行交通工具类的无参构造方法。");
    }
    Vehicle(int capacity) {
        this.capacity=capacity;
        System.out.println("执行交通工具的有参构造方法。");
    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public void print() {
        System.out.println("载客量:"+capacity);
    }
    public void run() {
        System.out.println("这是交通工具run方法");
    }
    
}

汽车类

package 交通工具;

public class Motor extends Vehicle {
    public int speed;
    public Motor() {
        speed=0;
        System.out.println("执行汽车类的无参构造方法。");

    }
    public Motor(int capacity,int speed) {
        super(capacity);
        this.speed=speed;
        System.out.println("执行汽车类的有参构造方法。");
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp(){
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print(){
        System.out.println("速度:"+speed);
        System.out.println("载客量:"+capacity);
    }
    public void run(){
        System.out.println("汽车正在公路上奔跑");
    }
    
}

轿车

package 交通工具;

public final class Car extends Motor {
    int capacity;
    public Car() {
        capacity=20;
		System.out.print("执行轿车类的无参构造方法。");
    }
    public Car(int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行轿车类的有参构造方法。");
    }
    public void run() {
        System.out.println("轿车速度为:"+speed);		
        System.out.println("轿车车载客量:"+capacity);
    }
    
}

公共汽车

package 交通工具;

public final class Bus extends Motor {
    public int capacity;
    public Bus() {
        capacity=20;
        System.out.println("执行公共汽车类的无参构造方法。");
    }
    public Bus(int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行公共汽车类的有参构造方法。");
    }
    public void run() {
        System.out.println("速度:"+speed);
        System.out.println("载客量:"+capacity);
    }
    
}

轮船类

package 交通工具;

public class Ship extends Vehicle {
    public int speed;
    //无参构造方法
    public Ship() {
        speed=0;
        System.out.println("执行汽车类的无参构造方法。");
    }
    public Ship(int capacity,int speed) {
        //调用父类的有参构造方法
        super(capacity);
        this.speed=speed;
        System.out.println("执行汽车类的有参构造方法。");
    }
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp() {
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print() {
        System.out.println("速度"+speed);
        System.out.println("载客量"+capacity);
    }
    public void run() {
        System.out.println("轮船正在大海上航行");
    }
    
}

航空母舰

package 交通工具;

public final class Birdfarm extends Ship{
    int capacity;
    public Birdfarm () {
        capacity=20;
		System.out.print("执行航空母舰类的无参构造方法。");
    }
    public Birdfarm (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行航空母舰类的有参构造方法。");
    }
    public void run() {
        System.out.println("航空母舰速度为:"+speed);		
        System.out.println("航空母舰车载客量:"+capacity);
    }
    
}

豪华游轮

package 交通工具;

public final class Luxuryship extends Ship{
    int capacity;
    public Luxuryship () {
        capacity=20;
		System.out.print("执行豪华游轮类的无参构造方法。");
    }
    public Luxuryship (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行豪华游轮类的有参构造方法。");
    }
    public void run() {
        System.out.println("豪华游轮速度为:"+speed);		
        System.out.println("豪华游轮载客量:"+capacity);	
    }
    
}

飞机类

package 交通工具;

public class Aeroplane extends Vehicle {
    public int speed;
    //无参构造方法
    public Aeroplane() {
        speed=0;
        System.out.println("执行汽车类的无参构造方法。");
    }
    public Aeroplane (int capacity,int speed) {
        //调用父类的有参构造方法
        super(capacity);
        this.speed=speed;
        System.out.println("执行汽车类的有参构造方法。");
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int speedUp() {
        speed=speed+10;
        return speed;
    }
    public int speedDown() {
        speed=speed-15;
        return speed;
    }
    public void print() {
        System.out.println("速度"+speed);
        System.out.println("载客量"+capacity);
    }
    public void run() {
        System.out.println("飞机在天上飞行");
    }
    
}

战斗机

package 交通工具;

public final class Fighter extends Aeroplane{
    int capacity;
    public Fighter  () {
        capacity=20;
		System.out.print("执行战斗机类的无参构造方法。");
    }
    public Fighter  (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行战斗机类的有参构造方法。");
    }
    public void run() {
        System.out.println("战斗机速度为:"+speed);		
        System.out.println("战斗机载客量:"+capacity);	
    }
    
}

客机

package 交通工具;

public final class Aircraft extends Aeroplane {
    int capacity;
    public Aircraft () {
        capacity=20;
		System.out.print("执行客机类的无参构造方法。");
    }
    public Aircraft (int capacity,int speed) {
        super(capacity,speed);
        this.capacity=capacity;
        System.out.println("执行客机类的有参构造方法。");
    }
    public void run() {
        System.out.println("客机速度为:"+speed);		
        System.out.println("客载客量:"+capacity);	
    }
    
}

猜你喜欢

转载自blog.csdn.net/qq_44859533/article/details/107579892