从 Java 中的基类访问子类字段
我有一个名为Geometry的基类,其中存在一个子类Sphere:
public class Geometry
{
String shape_name;
String material;
public Geometry()
{
System.out.println("New geometric 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;
}
}
我有一个包含所有Geometry对象的ArrayList,并且我想迭代它以检查文本文件中的数据是否正确读取。到目前为止,这是我的迭代器方法:
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);
}
}
}
如何访问并打印球体的半径和中心字段? 提前致谢 :)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想访问子类的属性,则必须强制转换为子类。
不过,这并不是最面向对象的做事方式:一旦您拥有更多几何子类,您将需要开始转换为每种类型,这很快就会变得一团糟。如果你想打印一个对象的属性,你应该在 Geometry 对象上有一个名为 print() 的方法或类似的方法,它将打印对象中的每个属性。像这样的事情:
这样,您不需要进行转换,只需在 while 循环内调用 g.print() 即可。
If you want to access properties of a subclass, you're going to have to cast to the subclass.
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:
This way, you don't need to do the casting and you can just call g.print() inside your while loop.
您必须 cast (具体来说,向下转型):
You have to cast (specifically, downcast):
我同意 rwhat 的观点,但与其实现自己的 print() 函数,不如通过重写 toString() 函数来避免向下转型,这可能会让您受益(并且更加面向对象)。
请注意,
check()
并不关心 g 是球体还是立方体。这将有助于最大限度地减少对instanceof
的调用。在球体中...
任何新形状(例如圆锥体)都将受益于 toString() 函数,但缺少它只会打印出几何体的版本。
I agree with rwhat, but instead of implementing your own print() function, it might benefit you (and be more Object Oriented) to avoid the downcasts by overriding the toString() function.
Note the
check()
doesn't care whether g is a Sphere or a Cube. This will help minimize the calls toinstanceof
.Over in Sphere...
Any new shape (e.g., Cone) would benefit by having the toString() function, but lacking it would just print out Geometry's version.
使用instanceof并转换为所需的子类。您可能希望将这些字段设为公共字段,或者使用具有 getter 和 setter 的私有字段的标准习惯用法。
use instanceof and Cast to the desired subclass. You might want to make those fields public or the standard idiom of private fields with getters and setters too.