首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

追问大神-向上转型和向下转型的实现。解决方法

2012-04-24 
追问大神---向上转型和向下转型的实现。package com.syq.testpublic class Shape {public void draw() {Sy

追问大神---向上转型和向下转型的实现。
package com.syq.test; 

public class Shape { 

  public void draw() { 
  System.out.println("绘制图形…………"); 
  } 

  public double getPerimter() { 
  return 0; 
  } 

  public double getArea() { 
  return 0; 
  } 

  public void print() { 
  System.out.println("这是父类的print方法"); 
  } 

}

 

package com.syq.test;

public class Triangle extends Shape {
  private double sid1;
  private double sid2;
  private double sid3;

  public Triangle() {
  }

  public Triangle(double sid1, double sid2, double sid3) {
  this.setSid1(sid1);
  this.setSid2(sid2);
  this.setSid3(sid3);
  }

// 求三边中最长的边
  public double sidMax() {
  return (sid1 > sid2) ? sid1 : (sid2 > sid3) ? sid2 : sid1;
  }

// 重写toString方法,用于描述对象
  public String toString() {
  return "三角形周长:" + this.getPerimter() + "\n三角形面积:" + this.getArea()
  + "\n最长的边长:"+this.sidMax();
  }

  public void draw() {
  System.out.println("绘制三角形…………三边长:"+this.getSid1()+" "+this.getSid2()+" "+this.getSid3());
  }

  // 绘制三角形,返回是否符合三角形的定义
  public void draw(Triangle triangle) {
  if (this.sid1 + sid2 > sid3 & sid3 - sid1 < sid2) {
  this.setSid1(sid1);
  this.setSid2(sid2);
  this.setSid3(sid3);
  System.out.println("成功绘制三角形!");
  }else {
  System.out.println("不符合绘制三角形的条件!");
  }
  }
// 计算周长
  public double getPerimter() {
  return this.sid1 + this.sid2 + this.sid3;
  }

// 计算三角形面积
  public double getArea() {
  double p = this.getPerimter() / 2;
  double area = Math.sqrt(p * (p - sid1) * (p - sid2) * (p - sid3));
  return p;
  }

  public double getSid1() {
  return sid1;
  }

  public void setSid1(double sid1) {
  this.sid1 = sid1;
  }

  public double getSid2() {
  return sid2;
  }  

  public void setSid2(double sid2) {
  this.sid2 = sid2;
  }

  public double getSid3() {
  return sid3;
  }

  public void setSid3(double sid3) {
  this.sid3 = sid3;
  }

}

 

package com.syq.test;

public class Circle extends Shape {
  private double r;

  public Circle() {
  }

  public Circle(double r) {
  this.setR(r);
  }

// 计算圆的直径
  public double diameter() {
  return 2 * r;
  }

  public void draw() {
  System.out.println("绘制圆…………半径:"+this.getR());
  }

// 绘制圆
  public void draw(Circle circle) {
  if (this.r > 0) {


  this.setR(r);
  System.out.println("成功绘制圆!");
  }else {
  System.out.println("绘制圆条件不符合!");
  }
  }

// 重写toString方法
  public String toString() {
  return "圆的半径: " + this.r + "\n圆的周长:" + this.getPerimter() + "\n圆的面积:"
  + this.getArea();
  }

// 计算圆的周长
  public double getPerimter() {
  return 2 * 3.1415926 * r;
  }

// 计算圆的面积
  public double getArea() {
  return r * r * 3.1415926;
  }

  public double getR() {
  return r;
  }

  public void setR(double r) {
  this.r = r;
  }

}

 

package com.syq.test;

public class Rectangle extends Shape {

  private double length;
  private double wide;

  public Rectangle() {
  }

  public Rectangle(double length, double wide) {
  this.setLength(length);
  this.setWide(wide);
  }

// 计算对角线
  public double diagonal() {
  return Math.sqrt(length * length + wide * wide);
  }

  public void draw() {
  System.out.println("绘制矩形…………长:"+this.getLength()+" 宽:"+this.getWide());
  }

// 绘制矩形
  public void draw(Rectangle rectangle) {
  if (this.length > 0 && this.wide > 0) {
  this.setLength(length);
  this.setWide(wide);
  System.out.println("成功绘制矩形!");
  } else {
  System.out.println("绘制矩形条件不符合!");
  }
  }

// 计算周长
  public double getPerimter() {
  return 2 * (this.length + this.wide);
  }

// 计算面积
  public double getArea() {
  return this.wide * this.length;
  }

// 重写toString方法
  public String toString() {
  return "矩形的周长: " + this.getPerimter() + "\n矩形的面积: " + this.getArea()
  + "\n矩形的对角线:" + this.diagonal();
  }

  public double getLength() {
  return length;
  }

  public void setLength(double length) {
  this.length = length;
  }

  public double getWide() {
  return wide;
  }

  public void setWide(double wide) {
  this.wide = wide;
  }

  }

 

package com.syq.test;

import java.util.Iterator;

public class TestShape {
  public static void main(String[] args) {
  Shape[] shapes = new Shape[3];

  shapes[0] = new Circle(1);
  shapes[1] = new Rectangle(1, 1);
  shapes[2] = new Triangle(1, 2, 1);

  for (int i = 0; i < shapes.length; i++) {

  // 到这步已经可以实现向上转型:父类 父类引用变量 = 子类对象
  Shape shape = shapes[i];
  shape.draw();


  System.out.println("面积:" + shape.getArea());
  System.out.println("周长:" + shape.getPerimter());

  //System.out.println( shape.toString );
  }
  }
}  

-------------------------------------------

在测试类中的for循环里面(红色字体),怎么才能调用到子类的方法呢?比如调用矩形Rectangle类中的diagonal()方法。麻烦大神帮我想想指点思路。在线等。

[解决办法]

Java code
if(shape instanceof Rectangle){    Rectangle r = (Rectangle)shape;    r.diagonal();} 

热点排行