Basic questions
Question 1: Concept Analysis
- What is class-to-class inheritance and what is its function?
- That is, the subclass inherits the attributes and behaviors of the parent class, so that the subclass object has the same attributes and behaviors as the parent class.
- The function is that the subclass reuses the content of the parent class.
- After inheritance, what impact do each member have between the parent class and the subclass?
- Member variables:
- No duplication of names, no impact.
- If you have the same name, use the nearest one, and use super to distinguish the parent class variable.
- Construction method:
- No impact, but the subclass constructor calls the parent class constructor by default
- Member methods:
- No duplication of names, no impact.
- Duplicate name, subclass overrides parent class method.
- Member variables:
- How to call members of parent class in subclass? How to use members of this class?
- Parent class member method: super.method name
- Non-private member variables of parent class: super.variable name
- Subclass member method: this.method name
- Subclass member variable: this.Variable name
- What is the difference between abstract methods and ordinary member methods?
- Abstract methods are modified with the abstract keyword and have no method body.
- Member methods have method bodies.
- What is the difference between abstract classes and ordinary classes?
- method:
- Abstract classes can contain abstract methods and member methods.
- Ordinary classes cannot contain abstract methods, only member methods.
- Object:
- Abstract classes cannot create objects.
- Ordinary classes can create objects.
- method:
Question 2: Grammar exercises
-
Syntax points: inheritance, abstract class
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps:
- Define abstract class A, abstract class B inherits A, and ordinary class C inherits B
- In class A, define the member variable numa, assign it a value of 10, and abstract the showA method.
- In class B, define the member variable numb, assign it a value of 20, and abstract the showB method.
- In class C, define the member variable numc and assign it a value of 30. Override the showA method and print numa. Override the showB method and print numb. Define the showC method and print numc.
- In the test class, create the C object and call the showA method, showB method, and showC method.
-
Reference answer:
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);
}
}
Question 3: Grammar exercises
-
Syntax points: inheritance, abstract class
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps:
- Simulated Agricultural College Animal Healthcare System Information.
- Define the abstract Poultry class
- Private member variables: animal species (name), symptoms (symptom), age (age), cause (illness)
- Provides empty parameter and parameterized construction methods
- Member methods:
- Abstract method symptom (showSymptom)
- Basic information of common methods (showMsg)
- Provide setXxx and getXxx methods
- Define the common duck class (Duck)
- Provides empty parameter and parameterized construction methods
- Override the showSymptom method to print symptom information.
-
Reference answer:
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());
}
}
Question 4: Grammar exercises
-
Grammar point: inheritance
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps:
- Simulation teaching management system teacher and student information.
- Define the Person class.
- Attributes: name, age
- Construction method: no-parameter construction method, parameterized construction method
- Member methods: getXxx method, setXxx method, display basic information showMsg method
- Define the Teacher class and inherit from Person
- Attribute: subject
- Construction method: no-parameter construction method, parameterized construction method
- Member methods: getXxx method, setXxx method, lecture method
- Define the Student class and inherit from Person
- Attribute: Score
- Construction method: no-parameter construction method, parameterized construction method
- Member methods: getXxx method, setXxx method, examination method
-
Reference answer:
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 +"分");
}
}
Question 5: Grammar exercises
-
Grammar point: inheritance
-
Write the code step by step, and the effect is as shown in the figure:
-
Writing steps
- Sim car website information.
- Define the car Auto class
- Attributes: brand, driver, price
- Define SUV to inherit Auto class
- Attributes: Standard value of small car commander: 4295, standard value of medium-sized car commander: 5070.
- Define the method of judging car models
- Judging small cars: smaller than the standard length of small cars
- Judging large vehicles: greater than the standard value of vehicle length for medium-sized vehicles
- To judge a medium-sized car: greater than the standard value of the length of a small car and less than or equal to the standard value of a medium-sized car.
- In the test class, create several SUV objects, save them to a collection, traverse the collection, and output a medium-sized SUV.
-
Reference answer:
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<SUV> 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;
}
}