AIDE_JAVA教程答案_第十五课(类和对象)

第十五课,了解类和对象。类是一种自定义数据类型,可用于描述客观事物,比如人、汽车等;而对象是类的实例,即类的具体化,比如汽车是类,李明的汽车就是对象,查看下面的程序,其中建立了一个新类”Rectangle”。

public class Main
{
	public static void main(String[] args)
	{
	}
}

class Rectangle
{
	int width;
	int height;
}

Rectangle类定义了两个整型字段:”width”和”height”。

public class Main
{
	public static void main(String[] args)
	{
	}
}

class Rectangle
{
	int width;
	int height;
}

在main()方法中我们利用”new Rectangle()”语句新建了一个Rectangle对象并把它赋给变量”rect”。

public class Main
{
	public static void main(String[] args)
	{
		Rectangle rect = new Rectangle();
	}
}

class Rectangle
{
	int width;
	int height;
}

我们设置了该矩形对象的”width”字段值为”100″并输出它。

public class Main
{
	public static void main(String[] args)
	{
		Rectangle rect = new Rectangle();
		
		rect.width = 100;
		System.out.println(rect.width);
		
	}
}

class Rectangle
{
	int width;
	int height;
}

现在设置Rectangle对象中”height”字段的值为”200″然后输出它。

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);
	}
}

class Rectangle
{
	int width;
	int height;
}

在main()方法中创建第二个Rectangle对象并将它赋给新变量”rect2″,然后将”width”设为”10″、”height”设为”20″,接着输出这两个字段值。

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;
}

定义一个新类”Point”,包含两个整型字段成员”x”和”y”。

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;
}

在main()方法中创建一个”Point”对象并将它赋给变量”p”。

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();
	}
}

class Rectangle
{
	int width;
	int height;
}

class Point{
    int x;
    int y;
}

设p对象的”x”为”4″、”y”为”5″,然后输出”p.x”和”p.y”的值。

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;
        System.out.println(p.x);
        p.y=5;
        System.out.println(p.y);
	}
}

class Rectangle
{
	int width;
	int height;
}

class Point{
    int x;
    int y;
}

在Rectangle类中添加”position”字段,变量类型为”Point”。

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;
	Point position;
}

在main()方法中将变量”p”赋给rect2类的”position”字段。

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);
		
        rect2.position=p;
	}
}

class Point
{
	int x;
	int y;
}

class Rectangle
{
	int width;
	int height;
	Point position;
}

最后输出”rect2.position.x”和”rect2.position.y”的值。

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);
		
        rect2.position=p;
        System.out.println(rect2.position.x);
        System.out.println(rect2.position.y);
	}
}

class Point
{
	int x;
	int y;
}

class Rectangle
{
	int width;
	int height;
	Point position;
}

版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明。
本文链接:https://www.aidemx.cn/1562.html

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/1562.html

梦雪

专注于AIDE教程分享

    留下你的评论

    *评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

    相关推荐