在main方法中创建一个新的Point对象,并把其分配给变量pimport javax.security.auth.x500.*;public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
}
}
class Rectangle
{
int width;
int height;
class Point
{
int x;
int y;
}}
因为Point类是Rectangle类的内部类,所以需要用内部类的创建方法新建Point类的对象p
Rectangle.Point p=rect.new Point();
完整的Java程序如下
import javax.security.auth.x500.*;public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
Rectangle.Point p=rect.new Point();
p.x=30;
p.y=40;
System.out.println(p.x);
System.out.println(p.y);
}
}
class Rectangle
{
int width;
int height;
class Point
{
int x;
int y;
}
}
运行结果
100
200
10
20
30
40
温馨提示:答案为网友推荐,仅供参考
第1个回答 2022-05-12
public class Main
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
Point p = new Point();
p.x = 4;
p.y = 5;
System.out.println(p.x);
System.out.println(p.y);
}
}
class Point
{
int x;
int y;
}
class Rectangle
{
int width;
int height;
}
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.width = 100;
System.out.println(rect.width);
rect.height = 200;
System.out.println(rect.height);
Rectangle rect2 = new Rectangle();
rect2.width = 10;
System.out.println(rect2.width);
rect2.height = 20;
System.out.println(rect2.height);
Point p = new Point();
p.x = 4;
p.y = 5;
System.out.println(p.x);
System.out.println(p.y);
}
}
class Point
{
int x;
int y;
}
class Rectangle
{
int width;
int height;
}