怎样用java编写创建对象和使用对象的方法程序

来源:百度知道 编辑:UC知道 时间:2024/05/07 09:03:10

写一个简单明了的给你
//此程序模拟二维坐标点,(x,y)
public class Point
{
public int x;
public int y;
public static void main(String[] args)
{
Point p1 = new Point(1,2);//创建一个坐标对象,也是创建了点p1(1,2)
Point p2 = new Point(2,5);//创建一个坐标对象,即创建了点p2(2,5)
//输出这两个点
System.out.println(p1);
System.out.println(p2);
}
//构造函数
public Point(int x,int y)
{
this.x =x;
this.y =y;
}
public String toString()//此函数在输出对象内容时调用
{ return "x:"+x+" y:"+y;
}

}

这个程序得到的结果是
x:1 y:2
x:2 y:5

用new声明呀,
类名 对象名=new 类名();
使用对象的方法用.
对象名.方法名();

public class Test{
public static void main(String[] args){
Bird bird = new Brid();
bird.setColor("red");
System.out.println("This is a " + bird.getColor() + "bird");
bird.fly();
}
}
class Bird{