继承和toString [英] Inheritence and toString

查看:155
本文介绍了继承和toString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

这是我的代码:

Point.java

Hi Everyone
This is my code:
Point.java

public class Point {
	
	protected double x;
	protected double y;
	
	public Point() 
	{
		
	}
	
	public Point(double x, double y)
	{
		this.x = x;
		this.y = y;
	}
	
	public Point(Point point)
	{
		x = point.x;
		y = point.y;
	}
	
	public Point add(Point z)
	{
		return new Point(x + z.x, y + z.y);
	}
	
	@Override
	public String toString()
	{
		return "Point: (" + x + "," + y + ")";
	}
}



Shape.java:


Shape.java:

public abstract class Shape {
	protected Point position;
	
	public void move(double x, double y){ 
		position.x = x;
		position.y = y;
	}
	
	public abstract void show();
}



Line.java:


Line.java:

public class Line extends Shape{
	
	Line(Point start, Point end)
	{
		position = new Point(start);
		this.end = new Point(end.x - start.x, end.y - start.y);
	}
	
	@Override
	public String toString()
	{
		return "Line: " + position + " to " + position.add(end);
	}
	
	public void show()
	{
		System.out.println("\n" + toString());
	}
	
	private Point end;
}



MainClass.java:


MainClass.java:

public class ShapesMain {

	public static void main(String[] args) {
		Shape shapes[] = new Shape[10];
		Point pts = null;
		Point p1 = null;
		Point p2 = null;
		double minRadius = 20.0;
		double maxRadius = 20.0;
		double maxDist = 100.0;
		int minPts = 2;
		int maxPts = 15;
		int choice = 0;
		
		for(int i = 0; i < 10; i++)
		{
			choice = (int) (Math.random() * 3);
			switch(choice)
			{
			case 0:
				p1 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				p2 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				shapes[i] = new Line(p1, p2);
				break;
			case 1:
				p1 = new Point((int) (Math.random()*maxDist), (int) (Math.random()*maxDist));
				shapes[i] = new Circle(p1, (int) (minRadius + (maxRadius - minRadius) * Math.random()));
				break;
			case 2:
				p1 = new Point((int) (Math.random() * maxDist), (int) (Math.random() * maxDist));
				p2 = new Point((int) (Math.random() * maxDist), (int) (Math.random() * maxDist));
				shapes[i] = new Rectangle(p1, p2);
				break;
			default:                                                      
		          System.out.println("\nInvalid shape choice = " + choice);
		          System.exit(1);
		          break;
			}
		}
		
		for(Shape shape : shapes)
		{
			shape.show();
		}
	}

}



样本输出:

行:点:( 53.0,50.0)到点:(54.0,36.0)



问题:

当我调用show方法(多态行为)时,如何点类中的toString()方法是否被调用?



如果shapes [1]是一行,我调用形状[1] .show,它会执行


Sample Output:
Line: Point: (53.0,50.0) to Point: (54.0,36.0)

Question:
When I call the show method(Polymorphic behavior), how does the the toString() method in the point class gets called?

If shapes[1] is a line and i call shapes[1].show, it executes

public void show()
    {
        System.out.println("\n" + toString());
    }



反过来调用toString()方法:


which in turn calles the toString() method:

@Override
	public String toString()
	{
		return "Line: " + position + " to " + position.add(end);
	}



如何将toString()中的位置替换为Point类中的toString()方法?



谢谢你


how does the position in the toString() gets replaced by toString() method in the Point class?

Thank you

推荐答案

请看我对这个问题的评论。如果我理解正确,你想了解机制,这是非常好的。请参阅:

http://en.wikipedia.org/wiki/Dynamic_dispatch [< a href =http://en.wikipedia.org/wiki/Dynamic_dispatchtarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Method_overriding [ ^ ],

http ://en.wikipedia.org/wiki/Virtual_method_table [ ^ ]。



请尝试理解它。如果很难根据这些信息制作出完整的图片,请询问;我会在一个简单的例子中展示这一点,但它应该真的更简单;你的东西在很多不相关的细节之下浑浊。



-SA
Please see my comment to the question. If I understood it correctly, you want to understand the mechanism, which is very good. Please see:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Method_overriding[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^].

Please try to understand it. If it is hard to make a whole picture based in this information, please ask; I would show this in a simple example, but it should be really much simpler; yours clouds the essence of things under a lot of unrelated detail.

—SA


这篇关于继承和toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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