클래스 정의 및 인스턴스(객체) 생성

~cpp 
import javax.swing.JOptionPane;
public class HelloWorld {
	public String word="Hello, World!"; 
	public void Hello()	{
		JOptionPane.showMessageDialog(null,word);
	}
	public void setName(String name)	{
		word=name;
	}
	
	public static void main()	{
		HelloWorld hello1= new HelloWorld();
		HelloWorld hello2;
		
		hello2=hello1;
		hello1.setName("Button");
		
		
	}

}

변수 및 메소드의 접근제어

~cpp 
//Line.java
import javax.swing.*;

public class Line {
	private Point p1 = new Point();
	private Point p2 = new Point();
	private String info = "";
	
	public Line()	{
	}
	public void setData(int p1_xValue, int p1_yValue, int p2_xValue, int p2_yValue) {
		p1.setX(p1_xValue);
		p1.setY(p1_yValue);
		p2.setX(p2_xValue);
		p2.setY(p2_yValue);
	}
	
	public void setInfo() {
		info += "Line point_1 x : " + p1.getX() + "\n" +
			  "Line point_1 y : " + p1.getY() + "\n" +
			  "Line point_2 x : " + p2.getX() + "\n" +
			  "Line point_2 y : " + p2.getY() + "\n";
	}
	
	public void draw() {
		setInfo();
		JOptionPane.showMessageDialog(null, info);
	}
	public void move(int xValue, int yValue) {
		p1.move(xValue, yValue);
		p2.move(xValue, yValue);
	}
	public void change(int p1_xValue, int p1_yValue, int p2_xValue, int p2_yValue) {
		move(p1_xValue, p1_yValue);
		move(p2_xValue, p2_yValue);		
	}	
	
}
~cpp 
//Rectangle.java
//Line.java
import javax.swing.*;

public class Rectangle {
	private Point p1 = new Point();
	private Point p2 = new Point();
	private String info = "";
	
	public Rectangle()	{
	}
	public void setData(int p1_xValue, int p1_yValue, int p2_xValue, int p2_yValue) {
		p1.setX(p1_xValue);
		p1.setY(p1_yValue);
		p2.setX(p2_xValue);
		p2.setY(p2_yValue);
	}
	
	public void setInfo() {
		info += "Rectangle point_1 x : " + p1.getX() + "\n" +
			  "Rectangle point_1 y : " + p1.getY() + "\n" +
			  "Rectangle point_2 x : " + p2.getX() + "\n" +
			  "Rectangle point_2 y : " + p2.getY() + "\n";
	}
	
	public void draw() {
		setInfo();
		JOptionPane.showMessageDialog(null, info);
	}
	public void move(int xValue, int yValue) {
		p1.move(xValue, yValue);
		p2.move(xValue, yValue);
	}
	public void change(int p1_xValue, int p1_yValue, int p2_xValue, int p2_yValue) {
		move(p1_xValue, p1_yValue);
		move(p2_xValue, p2_yValue);		
	}	
	
}
~cpp 
// MyPictureFrame.java

class MyPictureFrame {
	private Circle circle = new Circle();
	private Line line = new Line();
	private Rectangle rectangle = new Rectangle();	
	

	public MyPictureFrame() {
		circle.setData(100,100,50,50);
		circle.draw();
		line.setData(10,10,40,40);
		line.move(50,50);
		line.draw();
		rectangle.setData(150,150,240,240);
		rectangle.move(50,50);
		rectangle.draw();
		
	}
	public static void main(String[] args)	{
		MyPictureFrame frame = new MyPictureFrame();
	}
}

Thread

Retrieved from http://wiki.zeropage.org/wiki.php/JavaStudy2003/세번째과제/노수민
last modified 2021-02-07 05:23:31