Aide java类和对象教程_AIDE_JAVA教程答案_第十七课(方法)

第十七课,学习定义方法。一个对象包含3种最常见的成员:构造器(参考上一课)、数据和方法。数据用于定义状态,而方法是行为特征的抽象。看看这个程序,该Rectangle类有width字段和height字段以及构造函数,在main()方法中创建一个对象rect。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

}

我们在Rectangle类中增加方法getWidth(),该方法返回width字段的值。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

}

在main()方法中通过创建Rectangle对象对getWidth()方法进行调用,该对象被隐含地作为参数传递,在getWidth()方法中可以通过关键字this访问。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

}

现在,添加一个方法getHeight(),返回矩形的高度。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

int getHeight(){

return this.height;

}

}

在main()方法中调用getHeight()方法并打印结果。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

int getHeight(){

return this.height;

}

}

添加方法getArea()计算并返回矩形区域面积。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

int getHeight(){

return this.height;

}

int getArea(){

return this.width*this.height;

}

}

在main()方法中调用getArea()方法并打印结果。

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// The method

int getWidth()

{

return this.width;

}

int getHeight(){

return this.height;

}

int getArea(){

return this.width*this.height;

}

}

检查下面的方法void setWidth(int width),它设置一个Rectangle对象

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

rect.setWidth(10);

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// A setter method

void setWidth(int width)

{

this.width = width;

}

int getWidth()

{

return this.width;

}

int getHeight()

{

return this.height;

}

int getArea()

{

return this.width * this.height;

}

}

在main()方法中,我们以10作为参数调用setWidth()方法

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

rect.setWidth(10);

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

// A setter method

void setWidth(int width)

{

this.width = width;

}

int getWidth()

{

return this.width;

}

int getHeight()

{

return this.height;

}

int getArea()

{

return this.width * this.height;

}

}

现在添加方法setHeight(),设置矩形的高度

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

rect.setWidth(10);

rect.setHeight(20);

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

void setHeight(int height){

this.height=height;

}

void setWidth(int width)

{

this.width = width;

}

int getWidth()

{

return this.width;

}

int getHeight()

{

return this.height;

}

int getArea()

{

return this.width * this.height;

}

}

添加方法void scale(int factor),通过参数factor对矩形进行等比例缩放

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

rect.setWidth(10);

rect.setHeight(20);

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

void scale(int factor){

this.width=this.width*factor;

this.height=this.height*factor;

}

void setHeight(int height){

this.height=height;

}

void setWidth(int width)

{

this.width = width;

}

int getWidth()

{

return this.width;

}

int getHeight()

{

return this.height;

}

int getArea()

{

return this.width * this.height;

}

}

在main()方法中调用rect的scale()方法,以10作为比例因子,然后打印矩形的面积

public class Main

{

public static void main(String[] args)

{

Rectangle rect = new Rectangle(100, 200);

System.out.println(rect.getWidth());

System.out.println(rect.getHeight());

System.out.println(rect.getArea());

rect.setWidth(10);

rect.setHeight(20);

System.out.println(rect.getArea());

rect.scale(10);

System.out.println(rect.getArea());

}

}

class Rectangle

{

int width;

int height;

Rectangle(int width, int height)

{

this.width = width;

this.height = height;

}

void scale(int factor){

this.width=this.width*factor;

this.height=this.height*factor;

}

void setHeight(int height){

this.height=height;

}

void setWidth(int width)

{

this.width = width;

}

int getWidth()

{

return this.width;

}

int getHeight()

{

return this.height;

}

int getArea()

{

return this.width * this.height;

}

}

版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明。

本文链接:https://www.aidemx.cn/1591.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值