写一个MyPoint 完全封装类,其中含有私有的int类型的x 和y 属性,
分别用公有的getX 和setX、getY 和setY 方法访问,定义一个
toString 方法用来显示这个对象的x、y 的值,如显示(1,2),
最后用main 方法测试。
package javalearn;
class MyPoint{
private int x;
private int y;
public int getx() {
return x;
}
public int gety() {
return y;
}
public void setx(int x) {
this.x = x;
}
public void sety(int y) {
this.y = y;
}
public MyPoint(int x,int y){
this.x =x;
this.y=y;
}
public String toString(){
return "("+x+","+y+")";
}
}
public class testm{
public static void main(String []args){
MyPoint a=new MyPoint(1,2);
System.out.print(a);
}
}
一个源文件里只有一个public修饰的类,这个类此源文件首先进去的入口,这与main()函数的功能类似,因此把main()函数放在public修饰的类里面,由于使用main()函数测试,而且程序也是从main()函数开始运行的,因此在main()函数中new对象,然后初始化,然后就可以调用方法了。所以一般编程的顺序是首先设计类,然后在main()函数中new对象测试。
中间运行的时候还出现了一些错误,提示
解决的办法是在项目属性的运行设置里面,把预加载的项目给删除了
以上理解可能很片面,发出来希望高手看到一定指点。