从Java中的基类访问子类字段 [英] Access subclass fields from a base class in Java

查看:585
本文介绍了从Java中的基类访问子类字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Geometry 的基类,其中存在一个 子类:



< public class Geometry
{
String shape_name;
String material;

public Geometry()
{
System.out.println(New geometry object created。
}
}

和一个子类:

  public class Sphere extends Geometry 
{
Vector3d center;
double radius;

public Sphere(Vector3d coords,double radius,String sphere_name,String material)
{
this.center = coords;
this.radius = radius;
super.shape_name = sphere_name;
super.material = material;
}
}



我有一个ArrayList包含所有几何对象,我想迭代它来检查文本文件中的数据是否被正确读取。这是我的迭代器方法到目前为止:

  public static void check()
{
Iterator< Geometry> ; e = objects.iterator();
while(e.hasNext())
{
几何g =(几何)e.next();
if(g instanceof Sphere)
{
System.out.println(g.shape_name);
System.out.println(g.material);
}
}
}

如何访问和打印出现 半径和中心字段?
提前感谢:)

解决方案

如果你想访问子类的属性,以投射到子类。

  if(g instanceof Sphere)
{
Sphere s = ) G;
System.out.println(s.radius);
....
}

这不是最OO的方式做一些事情,虽然:一旦你有更多的子类的几何,你将需要开始投射到每个类型,这很快就变成了一个混乱。如果要打印对象的属性,您应该在您的几何对象上有一个名为print()的方法或者沿着这些行的方法,它将打印对象中的每个属性。像这样:

  
class Geometry {
...
public void print ){
System.out.println(shape_name);
System.out.println(material);
}
}

类形状extends几何{
...
public void print(){
System.out.println半径);
System.out.println(center);
super.print();
}
}

这样,你不需要你可以在你的while循环中调用g.print()。


I have a base class called Geometry from which there exists a subclass Sphere:

public class Geometry 
{
 String shape_name;
 String material;

 public Geometry()
 {
     System.out.println("New geometric object created.");
 }
}

and a subclass:

public class Sphere extends Geometry
{
 Vector3d center;
 double radius;

 public Sphere(Vector3d coords, double radius, String sphere_name, String material)
 {
  this.center = coords;
  this.radius = radius;
  super.shape_name = sphere_name;
  super.material = material;
 }
}

I have an ArrayList that contains all Geometry objects and I want to iterate over it to check whether the data from a text file is read in correctly. Here is my iterator method so far:

public static void check()
 {
  Iterator<Geometry> e = objects.iterator();
  while (e.hasNext())
  {
   Geometry g = (Geometry) e.next();
   if (g instanceof Sphere)
   {
    System.out.println(g.shape_name);
    System.out.println(g.material);
   }
  }
 }

How do I access and print out the Sphere's radius and center fields? Thanks in advance :)

解决方案

If you want to access properties of a subclass, you're going to have to cast to the subclass.

if (g instanceof Sphere)
{
    Sphere s = (Sphere) g;
    System.out.println(s.radius);
    ....
}

This isn't the most OO way to do things, though: once you have more subclasses of Geometry you're going to need to start casting to each of those types, which quickly becomes a big mess. If you want to print the properties of an object, you should have a method on your Geometry object called print() or something along those lines, that will print each of the properties in the object. Something like this:


class Geometry {
   ...
   public void print() {
      System.out.println(shape_name);
      System.out.println(material);
   }
}

class Shape extends Geometry {
   ...
   public void print() {
      System.out.println(radius);
      System.out.println(center);
      super.print();
   }
}

This way, you don't need to do the casting and you can just call g.print() inside your while loop.

这篇关于从Java中的基类访问子类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆