1. 什么叫做类与类的继承,作用是什么?
第二题:语法练习
语法点:继承,抽象类
编写步骤:
参考答案:
public class Test2 {
public static void main(String[] args) {
// 创建C对象
C c = new C();
// 调用c 中方法
c.showA();
c.showB();
c.showC();
}
}
abstract class A{
int numa = 10;
public abstract void showA();
}
abstract class B extends A{
int numb = 20;
public abstract void showB();
}
class C extends B{
int numc = 30;
@Override
public void showA() {
System.out.println("A类中numa:"+numa);
}
@Override
public void showB() {
System.out.println("B类中numb:"+numb);
}
public void showC(){
System.out.println("C类中numc:"+numc);
}
}
第三题:语法练习
语法点:继承,抽象类
编写步骤:
参考答案:
public class Test3 {
public static void main(String[] args) {
Duck duck = new Duck("鸭子", "感冒", "发烧", 2);
duck.showMsg();
duck.showSymptom();
}
}
/*
1.定义抽象家禽类(Poultry)
*/
abstract class Poultry {
// i.成员变量(私有):
private String name;
private String illness;
// 症状(symptom)
private String symptom;
// 年龄(age)
private int age;
// ii.成员方法: showSymptom
public abstract void showSymptom();
// 成员方法: showMsg
public void showMsg() {
System.out.print("动物种类:" + name);
System.out.println(",年龄:" + age + "岁");
System.out.println("入院原因:" + illness);
}
// iii.提供空参和带参构造方法
public Poultry() {
super();
}
public Poultry(String name, String illness, String symptom, int age) {
this.name = name;
this.illness = illness;
this.symptom = symptom;
this.age = age;
}
// iv.提供setXxx和getXxx方法
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
public String getSymptom() {
return symptom;
}
public void setSymptom(String symptom) {
this.symptom = symptom;
}
}
// Duck 类
class Duck extends Poultry {
public Duck() {
}
public Duck(String name, String illness, String symptom, int age) {
super(name, illness, symptom, age);
}
@Override
public void showSymptom() {
System.out.println("症状为:" + getSymptom());
}
}
第四题:语法练习
语法点:继承
编写步骤:
参考答案:
public class Test4 {
public static void main(String[] args) {
// i.创建老师对象t,并把名称赋值为”王小平”,年龄赋值为30,工资赋值为8000
Teacher t = new Teacher("王小平", 30, "Java");
// iii.调用老师对象t的讲解方法
t.teach();
// iv.创建学生对象 s,并把名称赋值为”李小乐”,年龄赋值为14,成绩赋值为90分.
Student s = new Student("李小乐", 14, 90);
// vi.调用学生对象 s 的考试方法
s.exam();
}
}
class Person {
// 名称(name)
private String name;
// 年龄(age)
private int age;
// 空参构造
public Person() {
}
// 带参构造
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// setXxx和getXxx方法
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;
}
}
/*
2.定义老师类(Teacher),继承Person类
*/
class Teacher extends Person {
// course(科目)
private String course;
// 空参构造
public Teacher() {
}
// 带参构造方法
public Teacher(String name,int age, String course) {
super(name,age);
this.course = course;
}
// 提供setXxx和getXxx方法
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public void teach() {
System.out.println(getName() +"老师,讲授"+course +"课");
}
}
/*
3.定义学生类(Student),继承Person类
*/
class Student extends Person {
// score(成绩)
private int score;
// 无参构造
public Student() {
super();
}
// 带参构造
public Student(String name, int age,int score) {
super(name, age);
this.score = score;
}
// 提供setXxx和getXxx方法
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void exam(){
System.out.println(getName()+"同学,考试得了:"+ score +"分");
}
}
第五题:语法练习
语法点:继承
编写步骤
参考答案:
public class Test5 {
public static void main(String[] args) {
// 创建SUV对象
SUV suv1 = new SUV(5079, 750000);
SUV suv2 = new SUV(4813, 760000);
SUV suv3 = new SUV(4270, 127800);
SUV suv4 = new SUV(4545, 188800);
//添加到集合中
ArrayList list = new ArrayList<>();
list.add(suv1);
list.add(suv2);
list.add(suv3);
list.add(suv4);
// 遍历集合,查询中型SUV
for (int i = 0; i < list.size(); i++) {
SUV suv = list.get(i);
if (suv.midSUV()){
suv.showMsg();
}
}
}
}
// 定义汽车类
class Auto {
private String type;
private double length;
private double price;
public Auto() {
}
public Auto(String type, double length, double price) {
this.type = type;
this.length = length;
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void showMsg() {
System.out.println("车型:" + type);
System.out.println("\t价格:" + price);
System.out.println("\t车长:" + length);
}
}
// 定义SUV类
class SUV extends Auto {
// 车长标准
private int miniLength = 4295;
private int midLength = 5070;
public SUV(double length, double price) {
super("SUV", length, price);
}
// 判断 小型车
public boolean miniSUV() {
return getLength() <= miniLength;
}
// 判断 大型车
public boolean largeSUV() {
return getLength() > midLength;
}
// 判断 中型车
public boolean midSUV() {
return getLength() > miniLength && getLength() <= midLength;
}
}