abstract class Animal{
String name;
String color = "动物色";
public Animal(String name){
this.name = name;
}
public abstract void run();
public static void eat(){
System.out.println("动物在吃..");
}
}
class Mouse extends Animal{
String color = "黑色";
public Mouse(String name){
super(name);
}
public void run(){
System.out.println(name+"四条腿慢慢的走!");
}
public static void eat(){
System.out.println("老鼠在偷吃..");
}
public void dig(){
System.out.println("老鼠在打洞..");
}
}
class Fish extends Animal {
public Fish(String name){
super(name);
}
public void run(){
System.out.println(name+"摇摇尾巴游..");
}
}
class Demo11
{
public static void main(String[] args)
{
Animal a = new Mouse("老鼠");
a.dig();
}
}
abstract class MyShape{
public abstract void getArea();
public abstract void getLength();
}
class Circle extends MyShape{
public static final double PI = 3.14;
double r;
public Circle(double r){
this.r =r ;
}
public void getArea(){
System.out.println("圆形的面积:"+ PI*r*r);
}
public void getLength(){
System.out.println("圆形的周长:"+ 2*PI*r);
}
}
class Rect extends MyShape{
int width;
int height;
public Rect(int width , int height){
this.width = width;
this.height = height;
}
public void getArea(){
System.out.println("矩形的面积:"+ width*height);
}
public void getLength(){
System.out.println("矩形的周长:"+ 2*(width+height));
}
}
class Demo12 {
public static void main(String[] args)
{
MyShape m = getShape(0);
变量类型要与返回值类型一致。
m.getArea();
m.getLength();
}
与周长。
public static void print(MyShape s){
s.getArea();
s.getLength();
}
public static MyShape getShape(int i){
if (i==0){
return new Circle(4.0);
}else{
return new Rect(3,4);
}
}
}