aide_java教程答案_第十六课(构造器)

第十六课,学习定义和使用构造器来初始化对象。查看下面的程序,其中建了一个Rectangle类。该类中还包含一个构造器(构造器也是一种特殊方法)并携带了一个参数width。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width)
	{
	}
}

在main()方法中我们新建了一个Rectangle对象并传入100给构造器。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width) 
	{
	}
}

现在在构造器中我们将参数width的值赋给当前类的width字段。关键字this指向当前类。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width)
	{
		this.width = width;
	}
}

最后我们输出Rectangle对象的width字段值。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width)
	{
		this.width = width;
		
	}
	
	
}

现在添加参数height值为200,并以逗号分隔。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
	}
	
	
}

然后通过构造函数的参数值初始化height字段。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
        this.height=height;
	}
	
	
}

在main()方法打印rect.height。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
        this.height=height;
	}
	
	
}

创建第二个Rectangle对象,宽度为10,高度为20,把它分配给变量rect2,并打印width和height字段。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
        this.height=height;
	}
	
	
}

添加另一个不带参数的构造函数初始化字段height和width的值为100。

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

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
        this.height=height;
	}
	
	Rectangle(){
        this.width=100;
        this.height=100;
    }
}

创建一个Rectangle对象,将其分配给变量rect3并打印width和height字段。

public class Main
{
	public static void main(String[] args)
	{
		Rectangle rect = new Rectangle(100,200);
		System.out.println(rect.width);
		System.out.println(rect.height);
        
        Rectangle rect2=new Rectangle(10,20);
        System.out.println(rect2.width);
        System.out.println(rect2.height);
        
        Rectangle rect3=new Rectangle();
        System.out.println(rect3.width);
        System.out.println(rect3.height);
	}
}

class Rectangle
{
	int width;
	int height;
	
	// A constructor
	Rectangle(int width,int height)
	{
		this.width = width;
        this.height=height;
	}
	
	Rectangle(){
        this.width=100;
        this.height=100;
    }
}

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

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

梦雪

专注于AIDE教程分享

    留下你的评论

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

    相关推荐