题目有关回答如下,供参考:
4、this:当前对象的引用
super:当前对象的超(父)类对象的一个引用
5、继承是面向对象最显著的一个特性。
继承的意义:继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。
定义继承:使用关键字 extends 来实现继承
6、多态(Polymorphism)按字面的意思就是“多种状态”,也是面向对象的一个特性。
允许将子类类型的引用赋值给父类类型的引用。
7、代码如下:
/**
* 父类(图形)
*
* @author qd
*
*/
abstract class Figure {
double area;
}
/**
* 求面积的接口
*
* @author qd
*
*/
interface Area {
public void getArea();
}
/**
* 三角形
*
* @author qd
*
*/
class Triangle extends Figure implements Area {
// 底
double bottom;
// 高
double high;
public Triangle(double bottom, double high) {
super();
this.bottom = bottom;
this.high = high;
}
@Override
public void getArea() {
area = bottom * high * 0.5;
System.out.println("三角形面积是:" + area);
}
}
/**
* 正方形
*
* @author qd
*
*/
class Square extends Figure implements Area {
// 边长
double length;
public Square(double length) {
super();
this.length = length;
}
@Override
public void getArea() {
area = length * length;
System.out.println("正方形面积是:" + area);
}
}
/**
* 圆
*
* @author qd
*
*/
class Circular extends Figure implements Area {
// 半径
double radius;
public Circular(double radius) {
super();
this.radius = radius;
}
@Override
public void getArea() {
area = Math.PI * radius * radius;
System.out.println("圆面积是:" + area);
}
}
public class Test {
public static void main(String[] args) {
// 三角形对象
Area triangle = new Triangle(3, 4);
triangle.getArea();
// 正方形对象
Area square = new Square(4);
square.getArea();
// 圆对象
Area circular = new Circular(2);
circular.getArea();
}
}
8、代码如下:
/**
* 球类
*
* @author qd
*
*/
class Ball {
// 私有成员变量半径
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
/**
* 台球
*
* @author qd
*
*/
class Billiards extends Ball {
// 私有成员变量颜色
private String color;
public void setColor(String color) {
this.color = color;
}
// 输出信息
public void printMess(Billiards billiards,Ball ball) {
System.out.println("台球的颜色是:" + billiards.color + " 台球的半径是:" + ball.getR());
}
}
/**
* 公有测试类
*
* @author qd
*
*/
public class TestBall {
public static void main(String[] args) {
Ball ball = new Ball();
ball.setR(5);
Billiards billiards = new Billiards();
billiards.setColor("白色");
billiards.printMess(billiards,ball);
}
}
7题和8题测试如下:
public interface Areable {
double getArea();
}
public abstract class Shape implements Areable {
private String name;
public Shape() {
}
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Shape [] shapes = {
new Triangle(3,4),
new Square(5),
new Round(6)
};
for (Shape shape : shapes) {
System.out.println(shape.getName() + "的面积为:" + shape.getArea());
}
}
}
import java.math.BigDecimal;
public class Triangle extends Shape {
/**
* 底
*/
private double base;
/**
* 高
*/
private double height;
public Triangle() {
super("三角形");
}
public Triangle(double base, double height) {
this();
this.base = base;
this.height = height;
}
@Override
public double getArea() {
//三角形的面积=底*高/2
//四舍五入保持两位小数
return BigDecimal.valueOf((base * height / 2)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
import java.math.BigDecimal;
public class Square extends Shape {
/**
* 边长
*/
private double length;
public Square() {
super("正方形");
}
public Square(double length) {
this();
this.length = length;
}
@Override
public double getArea() {
return BigDecimal.valueOf((length * length)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
}
import java.math.BigDecimal;
public class Round extends Shape {
private static final double PI = 3.1415926;
/**
* 半径
*/
private double radius;
public Round() {
super("圆形");
}
public Round(double radius) {
this();
this.radius = radius;
}
@Override
public double getArea() {
return BigDecimal.valueOf((PI * radius * radius)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}