javaoop-继承
我正在复制并粘贴我正在工作的网站上的代码javaoop-继承,java,class,oop,Java,Class,Oop,我正在复制并粘贴我正在工作的网站上的代码 * A Line composes of two Points - a begin point and an end point. */ public class Line { // The private instance variables Point begin, end; // Object members - instances of the Point class // Constructors public Line(int x
* A Line composes of two Points - a begin point and an end point.
*/
public class Line {
// The private instance variables
Point begin, end; // Object members - instances of the Point class
// Constructors
public Line(int x1, int y1, int x2, int y2) {
begin = new Point(x1, y1); // Construct the instances declared
end = new Point(x2, y2);
}
public Line(Point begin, Point end) {
this.begin = begin; // The caller constructed the instances
this.end = end;
}
// The public getter and setter for the private instance variables
public Point getBegin() {
return begin;
}
public Point getEnd() {
return end;
}
public void setBegin(Point begin) {
this.begin = begin;
}
public void setEnd(Point end) {
this.end = end;
}
public int getBeginX() {
return begin.getX(); // Point's getX()
}
public void setBeginX(int x) {
begin.setX(x); // Point's setX()
}
public int getBeginY() {
return begin.getY(); // Point's getY()
}
public void setBeginY(int y) {
begin.setY(y); // Point's setY()
}
public int[] getBeginXY() {
return begin.getXY(); // Point's getXY()
}
public void setBeginXY(int x, int y) {
begin.setXY(x, y); // Point's setXY()
}
public int getEndX() {
return end.getX(); // Point's getX()
}
public void setEndX(int x) {
end.setX(x); // Point's setX()
}
public int getEndY() {
return end.getY(); // Point's getY()
}
public void setEndY(int y) {
end.setY(y); // Point's setY()
}
public int[] getEndXY() {
return end.getXY(); // Point's getXY()
}
public void setEndXY(int x, int y) {
end.setXY(x, y); // Point's setXY()
}
// The toString() describe itself
public String toString() {
return "Line[begin=" + begin + ",end=" + end + "]";
// Invoke begin.toString() and end.toString()
}
public double getLength() {
return begin.distance(end); // Point's distance()
}
}
所以这里我们要讲的是公共类点和公共类线
在第一行中。。。。
正如我们在这里看到的,我们构造了这个类,它是一个构造函数,它的使用方式没有任何错误,或者它的逻辑不存在任何错误
但也有这种方法公共点。。。这不是Point类,所以我们可以在这里使用这个方法
我算出来了,因为点的行为类似于某种areturn类型,因为在这个方法中,我们不想得到begin,begin是一个点,一个在Point类中定义的对象。我明白了,这是我们开始的唯一方法。但一旦我们做到了
public static void main(String[] args) {
Line l1 = new Line(new Point(7,8), new Point(2,5));
构造函数开始在这里执行操作。因此,在最后形成了新的生产线。但是在构造函数中没有
return line;
或者有回程线;但我们看不见?我不知道。
然而,在以返回类型为点的方法中,存在一个
return point;
但是没有
return x;
在构造函数中,我很难理解这里的逻辑
顺便说一下,我们可以从这个方法中受益
public Point getBegin() {
return begin;
}
这样:
l1.getBegin().SOMEPOINTMETHOD;
但是如果我们不做这样的事情,只是开始做呢
l1.getBegin();
我们将获得什么样的产出?我知道输出类型将指向point,但因为point是一个对象,而且因为我们没有输入对象的实例,所以我们不会得到任何正确的结果?在这种情况下,Eclipse给出了一种奇怪的错误
我知道这已经很久了,但我只是想理解这里的逻辑,如果我太“愚蠢”,请不要马上判断-。-(与关闭此主题的顶部相同)构造函数是一种特殊的(类绑定的)方法,隐式创建类类型的实例。您必须使用
new
关键字检索此实例
如果使用非voidReturntype
的方法,则将执行方法体,并最终返回类型为Returntype
的对象。由您(例如,您的程序)来处理返回的对象,但您也可以忽略它
而且,最重要的是:这与继承没有任何关系 但是有返回类型之类的东西,所以
公共点xxx
实际上是一个普通的方法,而不是构造函数。就像方法public int getSize(){…}
一样,它是一个返回类型为int
的临时方法,但是在构造函数中没有返回行;“-构造函数没有返回类型。这样想吧,因为构造函数已经创建了该类的实例,所以您不需要显式返回。如果我是您,我会先阅读一些基本的Java阅读资料,比如这本>和这本,这也是关于要点:)>请相信我,如果您真的想学习Java,请开始阅读,无论你读得多好,都有很多书要读,所以你不妨从头开始:)!谢谢,尽管我的题目是错的,但你没有理会周围的人,而是帮了忙。谢谢:)