- 题1
- Java代码
- 题2
- Java代码
- 题3
- Java代码
- 加入默认构造函数
- Edition:2021/11/2
1.设计一个类DateInfo,要求其满足下述要求:
(1)要求有一个无参的构造函数,其初始的年、月、日分别为:2021,6,11。
(2)要求有一个带参数的构造函数,其参数分别对应年、月、日。
(3)要求用一个成员函数Set实现日期的设置。
(4)要求用一个成员函数Show实现输出日期。
public class DataInfo {
private int year;
private int month;
private int day;
public DataInfo(){
year=2021;
month=11;
day=2;
}
public DataInfo(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void set(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public void show(){
System.out.println(year);
System.out.println(month);
System.out.println(day);
}
}
题2
- 定义一个抽象类CShape,要求其满足下述要求:
(1)抽象类CShape要求有一个纯虚函数GetLength()。
(2)派生出四边型类CSquare和圆类CCircle。
(3)派生类要求有带参数的构造函数,其参数分别对应高、宽;半径。
(4)在派生类中重写函数GetLength()。
abstract public class CShape{
abstract public double getLength();
}
class CRect extends CShape{
private int height;
private int width;
public CRect(int height,int width){
this.height=height;
this.width=width;
}
public double getLength(){
return 2*(height+width);
}
}
class CCircle extends CShape{
private double radius;
public CCircle(int radius){
this.radius=radius;
}
public double getLength(){
return 2*3.14*radius*radius;
}
}
题3
- 定义RECT类(长方形)及其派生类CUB(长方体),具体要求如下:
(1)类RECT的成员如下:
(a)保护数据成员
l double x, y; 分别表示长方形的长和宽。
(b)公有成员函数
l RECT(double x1,double y1);构造函数,分别用x1、y1初始化x和y。
l virtual double area( ); 虚函数,计算长方形的面积,计算公式:面积=长×宽。
l double peri( ); 计算长方形的周长。计算公式:周长=2×长+2×宽。
l virtual int isSquare( ); 虚函数,判断是否为正方形,如是,返回1;否则返回0。
(2) 类CUB为类RECT的公有派生类,其成员如下:
(a) 私有数据成员
l double height; 表示长方体的高度。
(b) 公有成员函数
l CUB( ); 构造函数,用h、x、y分别初始化height及其基类成员x和y。
l double volume( ); 计算长方体的体积。计算公式:体积=底面积×高,其中底面积通过调用基类成员函数area( )计算。
l double area(); 计算长方体的表面积。计算公式:表面积=2×底面积+底面周长×高度。底面积和底面周长分别调用基类成员函数area()和peri()计算。
l int isSquare();判断是否为正方体,如是,返回1,否则返回0。在判断过程中,首先调用基类的成员函数isSquare()判断底面是否为正方形。
public class Rect{
protected double x;
protected double y;
public Rect(double x,double y){
this.x=x;
this.y=y;
}
public double area(){
return x*y;
}
public double peri(){
return 2*(x+y);
}
public boolean isSquare(){
if(x==y){
return true;
}
return false;
}
}
class Cub extends Rect{
private double height;
public Cub(double x,double y,double height){
super(x,y);
this.height=height;
}
public double volume(){
return x*y*height;
}
public double area(){
return 2*(x*y+x*height+y*height);
}
public boolean isSquare(){
if(super.isSquare()==true && x==height){
return true;
}
return false;
}
}
加入默认构造函数
public class Rect{
protected double x;
protected double y;
public Rect(double x,double y){
this.x=x;
this.y=y;
}
public Rect(){
}
public double area(){
return x*y;
}
public double peri(){
return 2*(x+y);
}
public boolean isSquare(){
if(x==y){
return true;
}
return false;
}
}
class Cub extends Rect{
private double height;
public Cub(){
}
public Cub(double x,double y,double height){
super(x,y);
this.height=height;
}
public double volume(){
return x*y*height;
}
public double area(){
return 2*(x*y+x*height+y*height);
}
public boolean isSquare(){
if(super.isSquare()==true && x==height){
return true;
}
return false;
}
}
创建一个子类的对象实例的时候,必先调用父类的无参数的构造函数(默认构造函数)。
假如父类有带参数的构造函数,那么系统将不会给它创建无参数的构造函数,这时,子类在实例化的时候,因为找不到父类的默认构造函数,编译器将会报错。
但如果在子类的构造函数中指定用父类的带参数的构造函数的时候,或者在父类中加一个无参数的构造函数,就不会报错。
Edition:2021/11/2一定要注意访问权限



