Java作为一种面向对象语言。支持以下基本概念:
* 多态
* 继承
* 封装
* 抽象
* 类
* 对象
* 实例
* 方法
* 重载
本节我们重点研究对象和类的概念。
* 对象:对象是类的一个实例,有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。
* 类:类是一个模板,它描述一类对象的行为和状态。
下图中男孩(boy)、女孩(girl)为类(class),而具体的每个人为该类的对象(object):
实验目的
(1)掌握Java类的结构、类的定义、方法和属性的定义以及对象的实现;
(2)掌握类及其成员修饰符的使用;掌握构造函数的使用;方法的参数传递和返回值的用法。
实验内容
(1)编写3个基本类: Triangle, Ladder和Circle,分别用来刻画“三角形”、“梯形”和“圆形”类; 1个主类:
Compute,负责计算每个形状的面积或周长。
具体要求:
* Triangle 定义3个变量:边长;和1个求周长的方法。
* public class Triangle { private double a; private double b; private double
c; Triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; }
Triangle(){ } public void setA(double a) { this.a = a; } public void
setB(double b) { this.b = b; } public void setC(double c) { this.c = c; }
public void getlong(){ System.out.print(a+b+c); } } }
* Ladder 定义3个变量:上底,下底和高;定义1个求面积的方法。
* public class Ladder { private double up; private double down; private
double heigh; Ladder(double up,double down,double heigh){ this.up=up;
this.down=down; this.heigh=heigh; } Ladder(){ } public void setUp(double up) {
this.up = up; } public void setDown(double down) { this.down = down; } public
void setHeigh(double heigh) { this.heigh = heigh; } public void getmeasure(){
System.out.print((up+down)*heigh/2); } }
* Circle 定义1个变量:半径;定义2个方法:求面积、求周长。
* public class Circle { private double R; Circle(double r){ R=r; } Circle(){
} public void setR(double r) { R = r; } public void getlong(){
System.out.print(2*Math.PI*R); } public void getmeasure(){
System.out.print(Math.PI*R*R); } }
* 3个基本类都要定义相应的构造方法,对变量进行初始化。
* public class Computer { public static void main(String[] args) { Triangle
triangle = new Triangle(10,10,10); Ladder ladder = new Ladder(5,6,7); Circle
circle = new Circle(3); System.out.print("\n三角形周长:\n"); triangle.getlong();
System.out.print("\n梯形面积:\n"); ladder.getmeasure();
System.out.print("\n圆形形周长:\n"); circle.getlong();
System.out.print("\n圆形面积:\n"); circle.getmeasure(); } } package 实验2; import
java.util.Scanner; class Triangle{ double x,y,z; public Triangle(double
x,double y,double z){ this.x=x; this.y=y; this.z=z; } public double
Perimeter(){ return x+y+z; } } class Ladder{ double up,down,h; public
Ladder(double up,double down,double h){ this.up=up; this.down=down; this.h=h; }
public double Area(){ return (up+down)*h/2; } } class Circle{ double r; public
Circle(double r) { this.r=r; } public double Area() { return Math.PI*r*r; }
public double Perimeter() { return 2*Math.PI*r; } } public class Computer {
double x,y,z,up,down,h,r; public static void main(String[] argn){ Computer
c=new Computer(); Scanner input=new Scanner(System.in);
System.out.println("请输入三角形边长:"); c.x=input.nextDouble();
c.y=input.nextDouble(); c.z=input.nextDouble(); Triangle t=new
Triangle(c.x,c.y,c.z); System.out.println("三角形周长为:");
System.out.println(t.Perimeter()); System.out.println("请输入梯形上底下底和高:");
c.up=input.nextDouble(); c.down=input.nextDouble(); c.h=input.nextDouble();
Ladder l=new Ladder(c.up,c.down,c.h); System.out.println("梯形的面积为:");
System.out.println(l.Area()); System.out.println("请输入圆的半径:");
c.r=input.nextDouble(); Circle ci=new Circle(c.r);
System.out.println("圆的面积为:"); System.out.println(ci.Area());
System.out.println("圆的周长为:"); System.out.println(ci.Perimeter()); } }
(2)编写一个账户类Account,它包括:一个名为id的int型账号码属性,一个名为balance的double型的账号余额属性,定义一个类型为java.util.Date的属性dateCreated,用于记录账号的创建日期。同时,定义无参的构造函数,一个名为withDraw的方法从账号提取特定数目的金额,一个名为deposit的方法向账号存入特定数目的金额。请编写测试程序,测试各个方法。
import java.util.Date; public class Account { private int id; private double
balance; private java.util.Date dateCreated; Account(int id,double balance){
this.id=id; this.balance=balance; this.dateCreated=new Date(); } public void
setId(int id) { this.id = id; } public void setBalance(double balance) {
this.balance = balance; } public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated; } public int getId() { return id; } public
double getBalance() { return balance; } public Date getDateCreated() { return
dateCreated; } public void withDraw(double sum){ balance-=sum; } public void
deposit(double sum){ balance+=sum; } public static void main(String[] args){
Account me=new Account(123,999); me.deposit(100); System.out.print("ID:\n");
System.out.print(me.getId()); System.out.print("\n当前余额为:\n");
System.out.print(me.getBalance()); System.out.print("\n时间:\n");
System.out.print(me.getDateCreated()); System.out.print("\n"); me.withDraw(30);
System.out.print("\n当前余额为:\n"); System.out.print(me.getBalance()); } } package
实验2; import java.util.Scanner; import java.util.Date; import
java.util.Calendar; class Account{ int id; double balance; Date dateCreated;
public Account(int id,double balance){ this.id=id; this.balance=balance;
this.dateCreated=Calendar.getInstance().getTime(); } public boolean
withDrown(double money){ if(this.balance>=money){ this.balance-=money; return
true; } else{ return false; } } public void deposit(double money){
this.balance+=money; } public double getbalance(){ return balance; } } public
class bank { int id; double balance,money1,money2; public static void
main(String[] argn){ bank c=new bank(); Scanner input=new Scanner(System.in);
System.out.println("请输入id:"); c.id=input.nextInt(); //存钱
System.out.println("请输入账户余额:"); c.balance=input.nextDouble(); Account ac=new
Account(c.id,c.balance); System.out.println("请输入提款数:");
c.money1=input.nextDouble(); boolean flag=ac.withDrown(c.money1); if(flag){
System.out.println("提款成功,当前账户余额为:"+ac.getbalance()); } else{
System.out.println("您的余额不足,提款失败,当前账户余额为:"+ac.getbalance()); } //取钱
System.out.println("请输入存款数:"); c.money2=input.nextDouble();
ac.deposit(c.money2); System.out.println("存款成功,当前账户余额为:"+ac.getbalance());
System.out.println("账号更新日期为为:"+ac.dateCreated); } }
(3)编写一个封装学生的类Student,能够描述学生的“学号”、“
姓名”、“性别”、“年龄”、“平均成绩”等基本属性,及获取属性、修改属性的方法和打印学生基本信息的print()方法。要求生成两个学生对象,在构造方法中进行初始化,并打印每个学生的基本信息。
public class Student { private int ID; private String name; private String
gender; private int age; private double Averagescore; Student(int ID,String
name,String gender,int age,double Averaescore) { this.ID=ID; this.name=name;
this.gender=gender; this.age=age; this.Averagescore=Averaescore; } Student(){ }
public void setAveragescore(double averagescore) { Averagescore = averagescore;
} public void setAge(int age) { this.age = age; } public void setGender(String
gender) { this.gender = gender; } public void setName(String name) { this.name
= name; } public void setID(int ID) { this.ID = ID; } public String getName() {
return name; } public int getID() { return ID; } public String getGender() {
return gender; } public int getAge() { return age; } public double
getAveragescore() { return Averagescore; } public void print(){
System.out.printf("姓名:%s 学号:%d 性别:%s 年龄:%d
平均成绩:%.1f",name,ID,gender,age,Averagescore); } public static void main(String[]
args) { Student 张三 = new Student(2203082,"张三","男",20,100); Student 李四 = new
Student(1234567,"李四","男",10,100); 张三.print(); System.out.print("\n");
李四.print(); } } package 实验2; class Student{ private int Id; private String
Name; private String Sex; private int Age; private double Aver_score; public
Student(int Id,String Name,String Sex,int Age,double Aver_score) {
this.setId(Id); this.setName(Name); this.setSex(Sex); this.setAge(Age);
this.setAver_score(Aver_score); } public int getId() { return Id; } public void
setId(int id) { Id = id; } public String getName() { return Name; } public void
setName(String name) { Name = name; } public String getSex() { return Sex; }
public void setSex(String sex) { Sex = sex; } public int getAge() { return Age;
} public void setAge(int age) { Age = age; } public double getAver_score() {
return Aver_score; } public void setAver_score(double aver_score) { Aver_score
= aver_score; } public void print() {
System.out.println("--"+this.getName()+"基本信息:"+"--");
System.out.println("学号:"+this.getId());
System.out.println("姓别:"+this.getSex());
System.out.println("年龄:"+this.getAge());
System.out.println("平均成绩:"+this.getAver_score()); } } public class Information
{ public static void main(String[] args) { Student student1=new
Student(001,"张三","女",20,89.2); student1.print(); Student student2=new
Student(002,"李四","男",19,90.4); student2.print(); } }