AIDE_JAVA教程答案_第十九课(类的继承)

第十九课,尝试使用继承的方法定义其他类(被称为父类)的子类。如何理解继承?列如水果和苹果的关系,我们可以说苹果继承了水果。检查该程序,Rectangle类有字段width,height和构造函数,在main()方法中创建对象并打印对象的面积。

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



class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

在Rectangle类下面添加类class Square {}

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



class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

class Square {
    
}

在类名Square之后写下extends Rectangle,以使Square类继承Rectangle类。

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

class Square extends Rectangle
{
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

该错误表示我们必须为类Square提供构造函数

我们仅仅添加了一个参数size

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

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

我们在Rectangle类中调用super构造函数并以size作为宽和高

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

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

在main()方法中以100作为参数新建一个Square对象,并将其分配给变量Square square

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

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

调用square.getArea()获得正方形面积并打印Square类中的getArea()方法继承自Rectangle类

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

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

现在改变变量rect为Rectangle类型尽管实际的对象是一个Square类型,您可以使用Rectangle继承类作为变量的类型,这个概念被称为多态。

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

class Square extends Rectangle
{
	public Square(int size)
	{
		super(size, size);
	}
}

class Rectangle
{
	private int width;
	private int height;
	
	public Rectangle(int width, int height) 
	{
		this.width = width;
		this.height = height;
	}
	
	public int getArea()
	{
		return width * height;
	}
}

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

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

梦雪

专注于AIDE教程分享

    留下你的评论

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

    相关推荐