这道题主要考察的是面对对象的知识。
Java:
public class Rectangle {
/*
* Define two public attributes width and height of type int.
*/
// write your code here
private Integer width;
private Integer height;
/*
* Define a constructor which expects two parameters width and height here.
*/
// write your code here
public Rectangle(int width,int height){
this.width = width;
this.height = height;
}
/*
* Define a public method `getArea` which can calculate the area of the
* rectangle and return.
*/
// write your code here
public int getArea(){
return width * height;
}
}
python:
class Rectangle:
def __init__(self,width,height)://注意self 还有__init__名字
self.width = width
self.height = height
def getArea(self):
return self.width * self.height