"Java Basic Introduction 2nd Edition"-Dark Horse Programmer's After-Class Answers and Detailed Explanations Chapter 4 Object Oriented (Part 2)

1. Fill in the blanks

	1、方法,抽象类
	2、子类、父类、基类
	3、final
	4、Object
	5、参数列表、“->”、表达式主体

2. True or false

1、错   2、对   3、错   4、对   5、错

Three, multiple choice questions

1、B     2、ABC      3、C      4、C      5、A

Four, short answer questions

1. In the inheritance relationship, the method of the subclass has the same method name, return type and parameter list as a method of the parent class, and the method of the subclass is said to override (override) the method of the parent class.

2. Polymorphism means that an object has multiple forms, which can be in different states under certain circumstances, corresponding to different attributes and methods. Simply put, polymorphism is the use of variables of the parent type to refer to the subclass object. According to the characteristics of the referenced subclass object, the program will get different operating effects.

3. In Java, a class modified with the abstract keyword is called an abstract class. An abstract class cannot be instantiated. It is usually necessary to write a subclass to inherit the abstract class and instantiate the subclass to obtain the object of the class. Abstract classes are usually used to represent an abstract concept.
An interface can be said to be a special abstract class. Only constants, abstract methods, static methods and default methods can be defined in the interface. Due to the particularity of the interface, the interface keyword must be used in the definition.

Five, programming questions

1.class Student {
    
    
	public String name;
	public int age;
	public Student(String name,int age){
    
    
		this.name=name;
		this.age=age;
	}
	public void show(){
    
    
		System.out.println("name: "+name+" age: "+age);
	}
}
class UnderGraduate extends Student{
    
    
	public String degree;
	public UnderGraduate(String name,int age,String degree){
    
    
		super(name, age);
		this.degree=degree;
	}
	public void show(){
    
    
		System.out.println("name: "+name+" age: "+age+" degree: "+degree);
	}
}
public class Test01{
    
    
	public static void main(String[] args) {
    
    
		Student student = new Student("zhangsan", 16);
		student.show();
		UnderGraduate underGraduate = new UnderGraduate("lisi", 20, "bechalor");
		underGraduate.show();
	}
	}
2.interface Shape {
    
    
	double area(double givenValue);
}
class Square implements Shape{
    
    
	public double area(double sideLength) {
    
    
      return sideLength*sideLength;
	}
} 
class Circle implements Shape{
    
    
	public double area(double r) {
    
    
       return Math.PI*r*r;
	}
}
public class Test02 {
    
    
	public static void main(String[] args) {
    
    
		Shape square = new Square();
		Shape circle = new Circle();
		System.out.println(square.area(2));
		System.out.println(circle.area(3));
	}
}

Six, the original question and its analysis

Nothing.

Guess you like

Origin blog.csdn.net/hypertext123/article/details/109297404