上机代码
Posted 0929-luoyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上机代码相关的知识,希望对你有一定的参考价值。
abstract class Shape{
String name;
public Shape(String name) {
this.name=name;
}
abstract void Length();
abstract void Area();
}
class Circle extends Shape{
double pi=3.14;
int r;
public Circle(String name,int r) {
super(name);
this.r=r;
}
public void Area(){
System.out.println(name+"的面积为:"+r*r*pi);
}
public void Length() {
System.out.println(name+"的周长为:"+r*2*pi);
}
}
class Rect extends Shape{
int width;
int higth;
public Rect(String name,int width,int higth) {
super(name);
this.width=width;
this.higth=higth;
}
public void Area(){
System.out.println(name+"的面积为:"+width*higth);
}
public void Length() {
System.out.println(name+"的周长为:"+(width+higth)*2);
}
}
class Square extends Shape{
double width;
public Square(String name,double width) {
super(name);
this.width=width;
}
public void Area(){
System.out.println(name+"的面积为:"+width*width);
}
public void Length() {
System.out.println(name+"的周长为:"+4*width);
}
}
String name;
public Shape(String name) {
this.name=name;
}
abstract void Length();
abstract void Area();
}
class Circle extends Shape{
double pi=3.14;
int r;
public Circle(String name,int r) {
super(name);
this.r=r;
}
public void Area(){
System.out.println(name+"的面积为:"+r*r*pi);
}
public void Length() {
System.out.println(name+"的周长为:"+r*2*pi);
}
}
class Rect extends Shape{
int width;
int higth;
public Rect(String name,int width,int higth) {
super(name);
this.width=width;
this.higth=higth;
}
public void Area(){
System.out.println(name+"的面积为:"+width*higth);
}
public void Length() {
System.out.println(name+"的周长为:"+(width+higth)*2);
}
}
class Square extends Shape{
double width;
public Square(String name,double width) {
super(name);
this.width=width;
}
public void Area(){
System.out.println(name+"的面积为:"+width*width);
}
public void Length() {
System.out.println(name+"的周长为:"+4*width);
}
}
public class Demo1 {
public static void main(String [] args) {
Shape c =new Circle("圆",4);
print(c);
Shape r = new Rect("矩形",5,6);
print(r);
Shape s =new Square("正方形",6);
print(s);
}
public static void print(Shape t) {
t.Length();
t.Area();
}
}
public static void main(String [] args) {
Shape c =new Circle("圆",4);
print(c);
Shape r = new Rect("矩形",5,6);
print(r);
Shape s =new Square("正方形",6);
print(s);
}
public static void print(Shape t) {
t.Length();
t.Area();
}
}
abstract class Shape{ String name; public Shape(String name) { this.name=name; } abstract void Length(); abstract void Area(); } class Circle extends Shape{ double pi=3.14; int r; public Circle(String name,int r) { super(name); this.r=r; } public void Area(){ System.out.println(name+"的面积为:"+r*r*pi); } public void Length() { System.out.println(name+"的周长为:"+r*2*pi); } } class Rect extends Shape{ int width; int higth; public Rect(String name,int width,int higth) { super(name); this.width=width; this.higth=higth; } public void Area(){ System.out.println(name+"的面积为:"+width*higth); } public void Length() { System.out.println(name+"的周长为:"+(width+higth)*2); } } class Square extends Shape{ double width; public Square(String name,double width) { super(name); this.width=width; } public void Area(){ System.out.println(name+"的面积为:"+width*width); } public void Length() { System.out.println(name+"的周长为:"+4*width); } } public class Demo1 { public static void main(String [] args) { Shape c =new Circle("圆",4); print(c); Shape r = new Rect("矩形",5,6); print(r); Shape s =new Square("正方形",6); print(s); } public static void print(Shape t) { t.Length(); t.Area(); } }
以上是关于上机代码的主要内容,如果未能解决你的问题,请参考以下文章