자바 언어에 대해 아직 비교적 새로운 언어이기 때문에 꽤 오랜 시간이 걸릴 것입니다.왜 개체 값이 아닌 초기 값이 변경됩니까?
이 프로그램에는 Point라는 클래스가 있습니다. 이렇게하면 모양의 정점에 대한 점이 만들어집니다.
이 클래스public class Point {
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
때문에 이러한 거리 등의 방법 (2 점 간의) 배율 (배 증가) (는 x를 취하고 y 좌표 및 this.x에 추가하고 XY) 변환하고있다 에.
그런 다음 3 개의 클래스를 추가하여 도형을 만듭니다. 삼각형, 직사각형, 원으로 구성되며, 생성자는 이와 같이 보입니다.
public class Triangle {
private int sides;
private Point[] points;
public Triangle(Point[] vertices) {
this.sides = 3;
this.points = vertices;
}
.
public class Rectangle {
private int sides;
private Point topCorner;
private int width;
private int length;
public Rectangle(Point corner, int w, int l){
this.sides = 4;
this.topCorner = corner;
this.width = w;
this.length = l;
}
및
public class Circle {
private Point center;
private int radius;
public Circle(Point center, int radius){
this.center = center;
this.radius = radius;
}
이러한 모든 클래스
같은 방법 번역이 있고, 규모. 그 예가 여기에 나와 있습니다.//For the Rectangle Class
public void translate(int dx, int dy){
topCorner.translate(dx, dy);
}
내 주 수업에 문제가 있습니다. 3 점을 만들고 3을 모두 배열로 사용하여 삼각형을 만들고 2 점을 직사각형과 원의 점으로 사용했습니다. 이제 모든 3 개의 객체에 대해 translate 메소드를 호출하면 값이 두 배가됩니다. 내 실수는 "p1" "p2"및 "p3"에 대한 참조를 어딘가에 전달했다고 생각하기 때문입니다. 오브젝트 값 대신 값을 변경합니다. 여기서 코드이고, I는
public class GraphicsDemo {
public static void main(String[] args) {
Point p1 = new Point(0,20);
Point p2 = new Point(20,0);
Point p3 = new Point(30,30);
Point[] vertices = {p1,p2,p3};
Triangle t = new Triangle(vertices);
Rectangle r = new Rectangle(p1,10,15);
Circle c = new Circle (p2, 25);
System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter());
System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter());
System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter());
c.translate(10, 15);
t.translate(10, 15);
r.translate(10, 15);
System.out.println("Triangle" + "\n" + t.toString() + "\n" + "area:" + t.area() + "\n" + "perimeter: " + t.perimeter());
System.out.println("Reactangle" + "\n" + r.toString() + "\n" + "area:" + r.area() + "\n" + "perimeter: " + r.perimeter());
System.out.println("Circle" + "\n" + c.toString() + "\n" + "area:" + c.area() + "\n" + "perimeter: " + c.perimeter());;
이것의 출력은
후 출력을 설명하는 것 대신에 점이 하나 이상의 사용되는 경우 (20, 30)가 추가되는 형태 좌표, 10 및 15를 추가 모양. 나는 이것이 실제로 오브젝트 대신에 포인트의 값을 변경하고 있기 때문에 이것이 확실하다고 생각합니다.하지만 이것을 변경하는 방법을 모른다.
미리 도움을 주실 수있는 분께 감사드립니다. 제가 많은 일을 시도하고 벽에 맞서 싸우는 경험이 처음입니다.