第四周课程总结&试验报告

实验二 Java简单类与对象
实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值

class Rectangle{
        double width;
        double height;
        String  color;
        public void tell(){
              System.out.println("宽度:"+width+",高度:"+height+",颜色:"+color);
        }
}
public class Rectangle1{
        public static void main(String args[]){
               Person per=null;
               per.width=10;
               per.height=10;
               per.color="黑色";
               per.tell();
        }
}

(2) 使用get…()和set…()的形式完成属性的访问及修改

class Rectangle{
        private double width;
        private double height;
        private String  color;
        public void tell(){
              System.out.println("宽度:"+width+",高度:"+height+",颜色:"+color);
        }
}
public double getWidth(){
         return width;
}
public void setWidth(double n)
          width=n;
}
public double getHeight(){
         return height;
}
public void setHeight(double a)
          height=a;
}
public String getColor(){
         return color;
}
public void setcolor(String b)
          color=b;
}
public class Rectangle1{
        public static void main(String args[]){
               Person per=new Person();
               per.setwidth=10;
               per.setheight=10;
               per.setcolor="黑色";
               per.tell();
        }
}

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

class Rectangle{
        private double width;
        private double height;
        private String  color;
        public Student(){
}
public Student(double width,double height,String  color){
        this.setWidth(width);
        this.setHeight(height);
        this.setColor(color);
}
        public void tell(){
              System.out.println("宽度:"+width+",高度:"+height+",颜色:"+color+",面积:”+area+",周长:"+length);
        }
}
public double getWidth(){
         return width;
}
public void setWidth(double n)
          width=n;
}
public double getHeight(){
         return height;
}
public void setHeight(double a)
          height=a;
}
public String getColor(){
         return color;
}
public void setcolor(String c)
          color=c;
}
public double area(){
        return width*height;
}
public double height(){
        return (width+height)*2;
public class Rectangle1{
        public static void main(String args[]){
               Person per=new Person();
               per.setwidth=10;
               per.setheight=10;
               per.setcolor="黑色";
               per.tell();
        }
}

2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

public class Account01{
         public static void main(String args[]){
         Student stu=null;
         stu =new Student();
         System.out.println("账号:"+stu.getLdentification());
         System.out.println("名字:"+stu.getName());
         System.out.println("日期:"+stu.getDate());
         System.out.println("余额:"+stu.getMoney());
}
class  Account02{
         private String Identification;
         private String name;
         private int date;
         private int password;
         private int money;
         public Student(){
         }
         public Student(String Identification,String name,int date,int password,int money){
               this.setLdentification(ldentification);
               this.setName(name);
               this.setDate(date);
               this.setPassword(password);
               this.setMoney(money);
         }
         public void setLdentification(Steing a){
               ldentification=a;
         }
         public void setName(String b){
               name=b;
         }
         public void setDate(int c){
               date=c;
         }
         public void setPassword(d){
               password=d;
         }
         public void setMoney(int e){
               money=e;
         }
         public getLdentification(){
               return ldentification;
         }
          public getName(){
               return name;
         }
          public getDate(){
               return date;
          }
          public getPassword(){
               return password;
          } 
          public getMoney(){
               money=money+e;
          }

实验过程(请自己调整格式)
总结:
1、类
在我的理解里面,java的类就是c语言中的函数,但是类的用法比函数要多一些,并且一个非常重要的就是类可以使数据私人化
2、String类
(1)、String首字母大写。
(2)、一个字符串就是一个String类的匿名对象。
(3)、使用“equals()”来比较字符串的内容,“==”是用来进行地址值的比较。
(4)、字符串的内容一旦声明则不可改变。
(5)、s=s.replace(a;b)把a的值替换成b。substring( ; )截取字符串。
3、包
(1)、管理文件,避免同名文件。
(2)、包的定义:package 包名称。子包名称;

posted on 2019-09-20 22:15  贺智超  阅读(159)  评论(0编辑  收藏  举报

导航