package shape;
public class Circle extends Shape{
private double r;
public Circle(double x, double y, double r) {
super(x,y);
this.r = r;
}
public Circle(){
super();
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public String toString() {
return "Circle{" +
"x=" + x +
", y=" + y +
", r=" + r +
'}';
}
@Override
public void show(){
System.out.println("圆的半径为:"+getR());
}
}
public class Rect extends Shape{
private double length;
private double width;
public Rect(double x,double y, double length, double width) {
super(x,y);
this.length = length;
this.width = width;
}
public Rect(){
super();
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public String toString() {
return "Rect{" +
"x=" + x +
", y=" + y +
", length=" + length +
", width=" + width +
'}';
}
@Override
public void show(){
System.out.println("矩形的长为:"+getLength()+",宽为:"+getWidth());
}
}
public class Shape {
double x;
double y;
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
public Shape(){}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void show(){
System.out.println("x="+x+",y="+y);
}
}
public class TestShape {
/**
* public static void printRect(Rect rect){
* rect.show();
* }
* public static void printCircle(Circle circle){
* circle.show();
* }
*
*/
//既能打印矩形也能打印圆形
public static void printShape(Shape shape){
shape.show();
}
//static修饰的方法只能调用静态的方法
public static void main(String[] args){
/**
* Shape shape=new Shape(3,4);
* shape.show();
* Circle circle=new Circle(1,2,4);
* circle.show();
* Rect rect=new Rect(1,2,3,4);
* rect.show();
*/
/**
* printRect(new Rect(1,2,3,4));
* printCircle(new Circle(1,2,4));
*/
//父类型 引用变量名=new 子类类型();
/**
* Shape shape=new Rect(1,2,3,4);
* shape.show();
* Shape shape1=new Circle(1,2,4);
* shape1.show();
*/
printShape(new Circle(1,2,4));
}
;
}
package account;
public abstract class Account {
private double balance;
public abstract double getInterest(double index,double time);
public Account(double balance) {
this.balance = balance;
}
public Account(){}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
public class AccountTest {
public static void main(String[] args){
Account acc=new FixedAccount(1000);
System.out.println("本金:"+acc.getBalance()+",利息为:"+acc.getInterest(0.003,1));
}
}
public class FixedAccount extends Account{
@Override
public double getInterest(double index, double time) {
return getBalance()*index*time;
}
public FixedAccount(double balance) {
super(balance);
}
public FixedAccount() {
}
}