尚学堂java 答案解析 第五章
本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改
一.选择题
1.AB
解析:A可以被所有类访问,B可以被当前包的所有类访问,也可以被所有子类访问
2.A
解析:所有子类会先用super()方法调用父类构造方法,再调用自己的构造方法
3.D
解析:上转型对象无法使用对象的新增方法,只能使用继承或重写的方法
4.ABD
解析:A.abstract类可以有abstract方法,也可以有非abstrct方法
B:非abstract类为abstract类的子类的话,必须重写父类方法,如果子类也是abstract类的话,可以继承或重写abstra方法
D:abstract类不能new对象,但可以成为子类的上转型对象,此时该对象可以调用子类的重写方法
5.AC
解析:B:final可以修饰接口变量,但不能修饰接口方法,因为接口的方法是是抽象方法,必须通过子类继承重写来实现
D:接口的方法一定是public的,以在其他类中实现
二.简答题
1.https://blog.csdn.net/claram/article/details/48002833
2.https://blog.csdn.net/qq_34834846/article/details/81413626
父类非抽象类:调用父类的构造方法,然后调用子类自己的构造方法(如果有)
父类为抽象类:(无参)调用父类的无参构造方法,然后调用自己的构造方法(如果有)
(有参)子类显示申明调用父类的有参构造函数,然后在调用子类的构造方法(如果有)
3.https://blog.csdn.net/sheepmu/article/details/38327205
上转型:Parents a = new Children();父类申明,子类实现,能使用子类隐藏或继承的变量,继承或重写的方法,不能使用子类新增的变量或方法;
下转型:a = (Children)a;子类由申明的父类强制转型为子类,可以使用子类的方法,但只有上转型对象才能使用下转型,纯父类对象是不能转为子类的对象,编译不会报错,但无法运行通过
4.final是为了封装而生的,final父类方法一旦申明,子类只能继承无法修改,用于具有普适性的方法和变量.final类无法被继承.
abtract是为了抽象而生的,abstract类的子类只要不是abstract类,必须重写父类的abstract类.abstract方法不允许实现(没有方法体)
5.https://blog.csdn.net/qq_34834846/article/details/81431103
==对基本数据类型比较的是值,对引用类型比较的是地址
equals()比较的是对象是否是同一个对象,即两个对象的地址是否一致;
三.编码题
1.
import java.math.BigDecimal;
import java.util.Scanner;
public class Circle {
private float radius = 0.0f;
final float PI = 3.14f;
Circle(){
System.out.println("调用父类的无参构造函数");
};
Circle(float r){
this.radius = r;
System.out.println("调用父类的有参构造函数"+r);
}
double getArea(){
return radius*PI*PI;
};
double getPerimeter(){
return 2*radius*PI;
};
void show(){
System.out.println("调用父类的show()函数");
System.out.printf("半径:%2.2f\n面积:%3.3f\n周长:%3.2f\n",radius,getArea(),getPerimeter());
}
}
class Cylinder extends Circle{
private double height = 0.0f;
Cylinder(float r,float height){
super(r);
this.height=height;
System.out.println("调用子类的有参构造函数");
};
double getVolume(){
return getArea()*height;
};
void showVolume(){
System.out.println("调用子类的show()函数");
System.out.printf("体积:%3.3f", getVolume());
}
};
class ch5_1{
public static void main(String[] args) {
float r = 0.0f;
float h = 0.0f;
double area = 0.0f;
double perimeter = 0.0f;
double volum = 0.0f;
Scanner input_r = new Scanner(System.in);
System.out.print("请输入半径:");
r=input_r.nextFloat();
Scanner input_h = new Scanner(System.in);
System.out.print("请输入高:");
h=input_h.nextFloat();
Circle circle = new Cylinder(r,h);
//调用父类的show
circle.show();
Cylinder cylinder = (Cylinder)circle;
cylinder.showVolume();
}
}
2.
public abstract class Instrument {
String name;
String sound;
abstract String makeSound();
}
class Erhu extends Instrument{
Erhu(){
name = "Erhu";
sound = "二胡声";
}
String makeSound(){
return sound;
}
}
class Piano extends Instrument{
Piano(){
name = "Piano";
sound = "钢琴声";
}
String makeSound(){
return sound;
}
}
class Violin extends Instrument{
Violin(){
name = "Violin";
sound = "小提琴声";
}
String makeSound(){
return sound;
}
}
class Musician{
void play(Instrument i){
System.out.println(i.makeSound());
};
}
class ch5_2{
public static void main(String[] args) {
Erhu erhu = new Erhu();
Piano piano = new Piano();
Violin violin = new Violin();
Musician musician = new Musician();
musician.play(erhu);
musician.play(piano);
musician.play(violin);
}
}
3.
public class Actor implements MovieStar,TVStar,Singer{
String name;
Actor(String name){
this.name = name;
System.out.println("大家好,我是"+name);
};
@Override
public void movieActor() {
System.out.println("我会演电影!");
}
@Override
public void tvActor() {
System.out.println("我会演电视!");
}
@Override
public void sing() {
System.out.println("我会唱歌!");
}
}
interface MovieStar{
void movieActor();
}
interface TVStar{
void tvActor();
}
interface Singer{
void sing();
}
class ch5_3{
public static void main(String[] args) {
Actor actor = new Actor("杨幂");
actor.movieActor();
actor.tvActor();
actor.sing();
}
}
.