不规则饼干
2015-03-07 23:21
//这是无参的 package com.LEE; public abstract class Shape { public abstract void perimeter(); public abstract void area(); } //************************************************ package com.LEE; public class Rectangle extends Shape { int a=1,b=2; @Override public void perimeter() { // TODO Auto-generated method stub System.out.println("矩形周长为"+2*(a+b)); } @Override public void area() { // TODO Auto-generated method stub System.out.println("矩形面积为"+a*b); } } //************************************************* package com.LEE; public class Circle extends Shape { int r=2; @Override public void perimeter() { // TODO Auto-generated method stub System.out.println("圆形周长是"+(2*3.14*r)); } @Override public void area() { // TODO Auto-generated method stub System.out.println("圆形面积是"+(3.14*r*r)); } } //**************************************************** package com.LEE; public class Initial { public static void main(String[] args){ Shape Rec=new Rectangle(); Shape Cir=new Circle(); Rec.perimeter(); Rec.area(); Cir.perimeter(); Cir.area(); } }
package com.imooc;
public abstract class Shape {
public abstract void area(double r);
public abstract void area(double a,double b);
public abstract void perimeter(double r);
public abstract void perimeter(double a,double b);
}
package com.imooc;
public class Circle extends Shape {
@Override
public void area(double r) {
// TODO Auto-generated method stub
System.out.println("圆的面积是:"+3.14*3.14*r);
}
@Override
public void area(double a, double b) {
// TODO Auto-generated method stub
}
@Override
public void perimeter(double r) {
// TODO Auto-generated method stub
System.out.println("圆的周长是:"+2*3.14*r);
}
@Override
public void perimeter(double a, double b) {
// TODO Auto-generated method stub
}
}
package com.imooc;
public class Rectangle extends Shape {
@Override
public void area(double r) {
// TODO Auto-generated method stub
}
@Override
public void area(double a, double b) {
// TODO Auto-generated method stub
System.out.println("矩形的面积是:"+a*b);
}
@Override
public void perimeter(double r) {
// TODO Auto-generated method stub
}
@Override
public void perimeter(double a, double b) {
// TODO Auto-generated method stub
System.out.println("矩形的周长是:"+2*(a+b));
}
}
package com.imooc;
public class Initial {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape cir=new Circle();
Shape rec=new Rectangle();
cir.area(3.0);
cir.perimeter(3.0);
rec.area(2, 3);
rec.perimeter(2, 3);
}
}
能文不武写的是错的
//父类抽象方法和子类方法实现,唯一的区别就是,父类抽象方法中{}是空的,哈哈O(∩_∩)O哈哈~ package com.LEE; public abstract class Shape { //在父类方法中,所带的参数都是在()里面声明的 public abstract void perimeter(int a,int b); public abstract void area(int r); } //************************************************ package com.LEE; public class Rectangle extends Shape { //int a=1,b=2; //同样,因为你这个方法已经是带参数的了,所以在方法里面就不需要定义变量的值了这个步骤应该放到main方法里面去做 @Override public void perimeter(int a, int b) { // TODO Auto-generated method stub System.out.println("矩形周长为"+2*(a+b)); } @Override public void area(int a, int b) { // TODO Auto-generated method stub System.out.println("矩形面积为"+a*b); } } //************************************************* package com.LEE; public class Circle extends Shape { //int r=2; @Override public void perimeter(int r) { // TODO Auto-generated method stub System.out.println("圆形周长是"+(2*3.14*r)); } @Override public void area(int r) { // TODO Auto-generated method stub System.out.println("圆形面积是"+(3.14*r*r)); } } //**************************************************** package com.LEE; public class Initial { public static void main(String[] args){ Shape Rec=new Rectangle(); Shape Cir=new Circle(); //我们可以在这里定义具体参数的值 int a=1,b=2 Rec.perimeter(a,b); Rec.area(a,b); int r=2 Cir.perimeter(r); Cir.area(r); } }
Java入门第二季 升级版
530696 学习 · 6091 问题
相似问题