public class Super {
public void print() {
System.out.println("Supeer --> print");
}
}
public class Test extends Super {
public void print() {
System.out.println("Test --> print");
}
public static void main(String[] args) {
Super t = new Super();
t.print();
}
}
A
D
class Animal{
public void eat(String food) {
}
public static void bark() {
}
public final void display() {
}
}
class Cat extends Animal {
//(1)
}
C
B
public class Parent1 {
Super(String s) {
System.out.println(s);
}
}
public class Test extends Parent2 {
Parent2() {
System.out.println("parent2");
}
}
public class Child extends Parent2 {
public static void main(String[] args) {
Child child = new Child();
}
}
B
public class Parent {
public String name;
Super(String pName) {
this.name = pName;
}
}
public class Test extends Parent {
public Test(String Name) { //2
name = "hello"; //3
super("kittv"); //4
}
}
C
C
public class Car {
String color;
String motor;
public Car(String color, String motor) { //1
this.color = color;
this.motor = motor;
}
}
public class Truck extends Car {
double weight;
public Truck() { //2
}
public Truck(String color, String motor, double weight) { //3
super(color,motor);
this.weight = weight;
}
public void display() {
System.out.println("颜色:" + color + "\t发动机的型号:" + motor + "\t载重量:" + weight );
}
public static void main(String[] args) {
Truck truck = new Truck("红色","玉柴",1.5); //4
truck.display();
}
}
B
A B D
D
public class Father {
private String name;
private int age;
public Father(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Father fa1 = new Father("晓明",13);
Father fa2 = new Father("晓明",13);
boolean flag = fa1.equals(fa2);
System.out.println(flag);
D
C
/**
* 父类:非机动车
**/
public class Vehicle {
//定义父类非机动车属性
private String logo;
private String color;
private int wheel;
private int seat;
//get/set方法
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWheel() {
return wheel;
}
public void setWheel(int wheel) {
this.wheel = wheel;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
//定义无参构造
public Vehicle() {
super();
this.setWheel(2);
this.setSeat(1);
}
//定义双参构造
public Vehicle(String logo, String color) {
super();
this.setLogo(logo);
this.setColor(color);
}
//定义四参构造
public Vehicle(String logo, String color, int wheel, int seat) {
super();
this.setLogo(logo);
this.setColor(color);
this.setWheel(wheel);
this.setSeat(seat);
}
//运行方法
public String info() {
String str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getLogo() + "牌的非机动车,有" + this.getWheel() + "个轮子,";
str += "有" + this.getSeat() + "个座椅";
return str;
}
}
/**
* 自行车类
**/
public class Bicycle extends Vehicle {
public Bicycle() {
super();
}
public Bicycle(String logo, String color) {
super(logo, color);
}
//重写运行方法
public String info() {
String str = "这是一辆" + this.getColor() + "颜色的,";
str += this.getLogo() + "牌的自行车";
return str;
}
}
/**
* 电动车类
**/
public class ElecBicycle extends Vehicle {
//新增电池属性
private String eleclogo;
//set/get方法
public String getEleclogo() {
return eleclogo;
}
public void setEleclogo(String eleclogo) {
this.eleclogo = eleclogo;
}
//无参构造
public ElecBicycle() {
super();
}
//有参构造
public ElecBicycle(String eleclogo) {
this.setEleclogo(eleclogo);
}
//重写运行方法
public String info() {
String str = "这是一辆使用" + this.getEleclogo() + "牌电池的电动车";
return str;
}
}
/**
* 三轮车类
**/
public class Tricycle extends Vehicle {
//无参构造
public Tricycle() {
super();
this.setWheel(3);
}
//重写运行方法
public String info() {
String str = "三轮车是一款有" + this.getWheel() + "个轮子的非机动车";
return str;
}
}
/**
* 测试类
**/
public class Test {
public static void main(String[] args) {
Vehicle v = new Vehicle("天宇","红",4,2);
System.out.println("父类信息测试" + v.info());
Bicycle b = new Bicycle("捷安特","黄");
System.out.println("自行车类信息测试" + b.info());
ElecBicycle e = new ElecBicycle("飞鸽");
System.out.println("电动车类信息测试" + e.info());
Tricycle t = new Tricycle();
System.out.println("三轮车类信息测试" + t.info());
}
}
/**
* Person类
**/
public class Person {
//私有属性:name(姓名)、age(年龄)、sex(性别)
private String name;
private int age;
private String sex;
//带参构造方法(name、age、sex为参数)
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
//无参构造方法
public Person() {
super();
}
//通过封装实现对属性的get/set方法设定
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//重写toString方法,表示形式为:姓名:+**+ 年龄:+**+ 性别:+**
public String toString() {
String str = "姓名:" + this.getName() + " ";
str += "年龄:" + this.getAge() + " ";
str += "性别:" + this.getSex();
return str;
}
}
/**
* 测试类
**/
public class Test {
public static void main(String[] args) {
Person p = new Person("李明",18,"男");
System.out.println(p);
System.out.println(p.toString());
}
}