第十七课,学习定义方法。一个对象包含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