掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
···C
class Rectangle{
private double width;
private double height;
private String color;
public Rectangle(String color,double width,double height){
this.setColor(color);
this.setWidth(width);
this.setHeight(height);
}
public String getColor(){
return color;
}
public void setColor(String c){
color=c;
}
public double getWidth(){
return width;
}
public void setWidth(double w){
width=w;
}
public double getHeight(){
return height;
}
public void setHeight(double h){
height=h;
}
public void getArea(){
System.out.println("面积:"+getWidth()getHeight());
}
public void getLength(){
System.out.println("周长:"+2(getWidth()+getHeight()));
}
public void getColor1(){
System.out.println("颜色:"+getColor());
}
}
public class zuoye002{
public static void main(String[] args) {
Rectangle re=new Rectangle("blue",4.00,2.00);
re.getArea();
re.getLength();
re.getColor1();
}
}
···
结果截图
标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、
姓名、开户日期、当前余额等信息。
···C
import java.util.Scanner;
import java.util.Date;
class Account {
private String id;
private String name;
private Date date;
private int password;
private int money;
public Account(String id, String name, int money) {
super();
this.id = id;
this.name = name;
this.money = money;
this.date = new Date();
this.password = 123456;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void put(int num) {
this.money = this.money+num;
}
public void out(int num) {
this.money = this.money-num;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public void () {
Scanner sc=new Scanner(System.in);
int password=sc.nextInt();
this.password = password;
sc.close();
}
}
···
原文:https://www.cnblogs.com/xia-unun/p/11559872.html