一、定义一个汽车类,构造方法创建对象,方法:加速减速停止(重载)
Car
package com.home.demo01;
public class Car {
private int num;
private String color;
private int size;
public Car(int num, String color, int size) {
this.num = num;
this.color = color;
this.size = size;
}
public Car() {
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void addSpeed() {
System.out.println("汽车加速");
}
public void subSpeed() {
System.out.println("汽车减速");
}
public void stop() {
System.out.println("汽车停车");
}
}
Test01
package com.home.demo01;
public class Test01 {
public static void main(String[] args) {
Car c = new Car(4, "红色", 100);
c.addSpeed();
c.subSpeed();
c.stop();
System.out.println(c.getColor() + "\t" + c.getNum() + "\t" + c.getSize());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0ced799751ef0c65e2f3e958b7334a8e.png)
二、创建两个包,创建两个类,实现继承,子类实现父类方法
MainClass
package com.home.demo02;
public class MainClass {
private int num;
public MainClass() {
}
public MainClass(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
MainSubClass
package com.home.demo03;
import com.home.demo02.MainClass;
public class MainSubClass extends MainClass {
public MainSubClass() {
}
public MainSubClass(int num) {
super(num);
}
}
Test02
package com.home.demo03;
public class Test02 {
public static void main(String[] args) {
MainSubClass s=new MainSubClass(18);
System.out.println(s.getNum());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f4f13a24b1ab93df08ecb8670560f671.png)
三、定义一个汽车类,属性:轮胎数,颜色,重量,速度;方法:加速,减速,停车。定义一个小汽车类,继承,添加属性:空调,CD;覆盖加减速方法。
Auto
package com.home.demo04;
public class Auto {
private int num;
private String color;
private int size;
private int speed;
public Auto(int num, String color, int size, int speed) {
this.num = num;
this.color = color;
this.size = size;
this.speed = speed;
}
public Auto() {
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void addSpeed(int add) {
}
public void subSpeed(int sub) {
}
public void stop() {
System.out.println("车停了");
}
}
Car
package com.home.demo04;
public class Car extends Auto {
private String cd;
private String kg;
public Car(int num, String color, int size, int speed, String cd, String kg) {
super(num, color, size, speed);
this.cd = cd;
this.kg = kg;
}
public Car() {
}
public String getCd() {
return cd;
}
public void setCd(String cd) {
this.cd = cd;
}
public String getKg() {
return kg;
}
public void setKg(String kg) {
this.kg = kg;
}
@Override
public void addSpeed(int add) {
setSpeed(getSpeed() + add);
}
@Override
public void subSpeed(int sub) {
setSpeed(getSpeed() - sub);
}
}
Test03
package com.home.demo04;
public class Test03 {
public static void main(String[] args) {
Car car=new Car(4,"红",12,0,"周杰伦","格力");
car.addSpeed(10);
System.out.println(car.getSpeed());
car.subSpeed(5);
System.out.println(car.getSpeed());
car.stop();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6d22031d043e28abd36ac542e1875c68.png)
四、定义车类为抽象类,两个子类继承车类实现抽象方法输出不同的信息
Vehicle
package com.home.demo05;
public abstract class Vehicle {
public abstract String noOfWheels();
}
Car
package com.home.demo05;
public class Car extends Vehicle{
@Override
public String noOfWheels() {
return "四轮车";
}
}
Motorbikes
package com.home.demo05;
public class Motorbikes extends Vehicle{
@Override
public String noOfWheels() {
return "双轮车";
}
}
Test04
package com.home.demo05;
public class Test04 {
public static void main(String[] args) {
Car c=new Car();
Motorbikes m=new Motorbikes();
System.out.println(c.noOfWheels());
System.out.println(m.noOfWheels());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/ede5b0d0536e8704e0da698a13360b5b.png)
五、定义一个接口,两个带参方法;定义两个类实现该接口的两个方法。
Vehicle
package com.home.demo06;
public interface Vehicle {
void start(int num);
void stop(int num);
}
Bike
package com.home.demo06;
public class Bike implements Vehicle{
@Override
public void start(int num) {
System.out.println(num);
}
@Override
public void stop(int num) {
System.out.println(num);
}
}
Car
package com.home.demo06;
public class Car implements Vehicle{
@Override
public void start(int num) {
System.out.println(num);
}
@Override
public void stop(int num) {
System.out.println(num);
}
}
Test05
package com.home.demo06;
public class Test05 {
public static void main(String[] args) {
Bike b=new Bike();
Car c=new Car();
b.start(10);
b.stop(5);
c.start(20);
c.stop(15);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8c1d905d45411facf0b1fcf3e154b231.png)