抽象:使用abstract声明的类叫做抽象类,如果一个类里有一个或者多个抽象方法,类就必须指定成abstrat(抽象)
1.有一个抽象方法可以定义成抽象类
2.抽象类可以定义成抽象方法和属性
3.抽象类是不能实例化
4.抽象类可以有构造方法
package com.lanou.obj;
public class test7 {
public static void main(String[] args) {
square squ = new square("aa", 10);
double area = squ.area();
System.out.println("squ的名字:"+squ.getName() + "面积-为:"+area);
Rectangular rect = new Rectangular(10,12,"bb");
System.out.println("rect的名字:"+rect.getName()+"面积为:"+rect.area());
}
//Shape shape = new Shape();
}
class square extends shape{
//边长
double siadelength;
@Override
double bc() {
// TODO Auto-generated method stub
return this.siadelength * 4;
}
@Override //面积
double area() {
return this.siadelength * this.siadelength;
}
public square(String name,double sidlength){
super(name);
this.siadelength = sidlength;
}
}
class Rectangular extends shape{
double length;
double width;
@Override
double bc() {
// TODO Auto-generated method stub
return (this.length+this.width)*2;
}
@Override
double area() {
// TODO Auto-generated method stub
return this.length * this.width;
}
public Rectangular(){
}
public Rectangular(int length,int width,String name){
super(name);
this.length = length;
this.width = width;
}
}
abstract class shape{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
abstract double bc();
abstract double area();
public shape() {
}
public shape(String name){
this.name = name;
}
}
网友评论