首页 > 其他分享 >构造器(有参、无参)

构造器(有参、无参)

时间:2023-02-14 16:32:50浏览次数:28  
标签:无参 capacity System 构造 println speed public out


构造器: 就是和类名相同但无返回类型的方法。用于当前或某一对象的实例化,并将当前或某一对象返回。无参构造: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);
}

}


标签:无参,capacity,System,构造,println,speed,public,out
From: https://blog.51cto.com/u_14935708/6057230

相关文章

  • 稳妥构造函数模式
    /*稳妥对象(durableobjects)*所谓稳妥对象,指的是没有公共属性,而且其方法也不引用this对象。*稳妥模式最适合在一些安全环境中(这些环境会禁止使用this和new),*或......
  • 寄生构造函数模式
    /*感觉没多大用记下得了*//**基本思维:*创建一个函数,该函数的作用仅是封装创建对象的代码,然后返回新创建的对象。*表面上看很像构造函数**/func......
  • 类的构造函数和析构函数
    构造函数和析构函数构造函数是类的入口函数析构函数是类的销毁函数1、构造函数a、构造函数默认是public类型的,如果定义private则定义外部不能进行对象的创建,所以只能是......
  • C++构造和析构
    category:cpp参考书籍:C++PrimerEssentialC++编译器:gcc/g++C++构造和析构构造函数名字和类名相同没有返回值构造函数是用来构造对象,构造对象时候必定调用构造函数不......
  • java学习第十三天笔记-面向对象242-子类能继承父类的那些内容1构造方法不能被继承下来
       ......
  • 拷贝构造函数
    拷贝构造函数用一个已存在的对象创建新的对象,不会调用(普通)构造函数,而是调用拷贝构造函数。如果类中没有定义拷贝构造函数,编译器将提供一个拷贝构造函数,它的功能是把已存......
  • 构造函数与析构函数
    构造函数和析构函数构造函数:在创建对象时,自动的进行初始化工作。析构函数:在销毁对象前,自动的完成清理工作。1)构造函数语法:类名()l访问权限必须是public。l函数名必......
  • Spring构造函数
    Spring中的一个bean,需要实例化得到一个对象,而实例化就需要用到构造方法。一般情况下,一个类只有一个构造方法:要么是无参的构造方法要么是有参的构造方法如果只有一个......
  • 原型 / 构造函数 / 实例
    原型(prototype):一个简单的对象,用于实现对象的属性继承。可以简单的理解成对象的爹。在Firefox和Chrome中,每个JavaScript对象中都包含一个_proto_(非标准)的属性指向它爹(该......
  • 前端学习案例6-构造函数继承
    ......