zoukankan      html  css  js  c++  java
  • Java第三次作业——面向对象基础(封装)

    Java第三次作业——面向对象基础(封装)

    (一)学习总结

    1.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。

    封装性就是指对外部不可见,用对象直接访问类中的属性,在面向对象法则中是不允许的。

    属性封装:private 属性类型 属性名称;

    方法封装:private 方法返回值 方法名称(参数列表){}

    举例:

    class Days{
    private int nian;
    private int yue;
    private int ri;
    
    public Days(){
    	
    }
    public Days(int nian,int yue,int ri){
    	this.nian=nian;
    	this.yue=yue;
    	this.ri=ri;
    }
    public String toString(){
    	return this.nian+"-"+this.yue+"-"+this.ri;
    }
    
    
    public int getNian(){
    	return nian;
    }
    public void setNian(int nian){
    	this.nian=nian;
    }
    
    public int getYue(){
    	return yue;
    }
    public void setYue(int yue){
    	this.yue=yue;
    }
    
    public int getRi(){
    	return ri;
    }
    public void setRi(int ri){
    	this.ri=ri;
    }
    }
    
    public class Test {
     public static void main(String args[]){
    	  Days tian=new Days(1997,10,30);
    System.out.println("时间:"+tian.toString());
          }
    }
    

    2.阅读下面程序,分析是否能编译通过?如果不能,说明原因。

    (1)

    class A{
    private int secret = 5;
    }
    public class Test{
    public static void main(String args[]){
        A a = new A();
        System.out.println(a.secret++);
    }
    }
    

    不能编译通过,A.secret不可视,应修改为:

    class A{
    private int secret = 5;
    
    public int getSecret() {
    	return secret;
    }
    
    public void setSecret(int secret) {
    	this.secret = secret;
    }
    }
    public class Test{
    public static void main(String args[]){
        A a = new A();
        System.out.println(a.getSecret());
    }
    }
    

    (2)

    public class Test{
    int x = 50;
    static int y = 200;
    public static void method(){
        System.out.println(x+y);
    }
    public static void main(String args[]){
        Test.method();
    }
    }
    

    不能编译通过,不能对非静态字段 x 进行静态引用,应修改为:

    public class Test{
    static int x = 50;
    static int y = 200;
    public static void method(){
        System.out.println(x+y);
    }
    public static void main(String args[]){
        Test.method();
    }
    }
    

    3 . 使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出了测试类代码和Book类的部分代码,将代码补充完整。
    class Book{
    int bookId;
    String bookName;
    double price;
    private static int sum;
    private static int zui;
    static{
    sum=0;
    zui=1000;
    }
    public Book(){

    }
    public Book(String bookName,double price){
    	this.bookName=bookName;
    	this.price=price;
    	 bookId=zui;
         inHE();
         inU();
    }
    public int getBookId() {
        return bookId;
    }
    
    public void setBookId(int bookId) {
        this.bookId = bookId;
    }
    
     public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    } 
    static void inU(){
        sum++;
    }
    static void inHE(){
        zui++;
    }
    public static int totalBook(){
        return zui-1000;
    }
    public String toString(){
        return "书号"+this.getBookId()+"书名"+this.getBookName()+"价格"+this.getPrice();
    }
    
    }
     public class Test{
    public static void main(String args[]){ 
        Book[] books = {new Book("c语言程序设计",29.3),
                        new Book("数据库原理",30),
                        new Book("Java学习笔记",68)};
        System.out.println("图书总数为:"+ Book.totalBook()); 
        for(Book book:books){
            System.out.println(book.toString());
        }		
    }   
    }
    

    4.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。

    单例模式确保一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例。

    优点:

    1,一个类只能有一个实例

    2,自己创建这个实例

    3,整个系统都要使用这个实例

    5.理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由。

    public class Test {
    String str = new String("你好  ");
    char[] ch = { 'w','o','l','l','d' };
    public static void main(String args[]) {
        Test test = new Test();
        test.change(test.str, test.ch);
        System.out.print(test.str);
        System.out.print(test.ch);
    }
    public void change(String str, char ch[]) {
        str = "hello";
        ch[0] = 'W';
    }
    }
    

    运行结果为:你好 Wolld

    按地址传递 并将地址为0的字母大写,java只支持参数传值的机制, 即方法得到的是所有参数值的一个副本,方法不能修改传递给它的任何参数变量的内容。change调用的是str的副本,不能改变str的内容。

    6.其他需要总结的内容。

    (二)实验总结

    1.按照下列要求完成类的设计

    (1)设计一个日期类,用于记录年、月、日,提供对日期处理的常用方法(日期输出格式xxxx-xx-xx)。

    (2)设计一个职工类,该职工类至少具有下面的属性:职工号,姓名,性别,生日,工作部门,参加工作时间,设计相应的方法。

    (3)设计一个部门类,包括下列属性:部门编号,部门名称,经理,设计相应方法。

    (4)设计一个测试类,采用对象数组模拟一个拥有两个部门,10个职工的单位,实现对相关信息的查询功能。

    程序

    package 部门员工;
    class Days{
    private int nian;
    private int yue;
    private int ri;
    
    public Days(){
    	
    }
    public Days(int nian,int yue,int ri){
    	this.nian=nian;
    	this.yue=yue;
    	this.ri=ri;
    }
    public String toString(){
    	return this.nian+"-"+this.yue+"-"+this.ri;
    }
    public int getNian(){
    	return nian;
    }
    public void setNian(int nian){
    	this.nian=nian;
    }
    
    public int getYue(){
    	return yue;
    }
    public void setYue(int yue){
    	this.yue=yue;
    }
    
    public int getRi(){
    	return ri;
    }
    public void setRi(int ri){
    	this.ri=ri;
    }
    }
    
    class Zhi{
    private String num;
    private String ming;
    private String sex;
    private Days datatime;
    private int time;
    private Bumen gongzuo;
    
    
    public Zhi(){
    	
    }
    public Zhi(String num,String ming,String sex,Days datatime,int time,Bumen gongzuo){
    	this.num=num;
    	this.ming=ming;
    	this.setSex(sex);
    	this.setTime(time);
    	this.datatime=datatime;
    	this.gongzuo=gongzuo;
    }
    
    
    public String getNum(){
    	return num;
    }
    public void setNum(String num){
    	this.num=num;
    }
    public String getMing(){
    	return ming;
    }
    public void setMing(String ming){
    	this.ming=ming;
    }
    public String getSex(){
    	return sex;
    }
    public void setSex(String sex){
    	this.sex=sex;
    }
    public Days getDatatime(){
    	return datatime;
    }
    public void setDatatime(Days datatime){
    	this.datatime=datatime;
    }
    
    public Bumen getGongzuo(){
    	return gongzuo;
    }
    public void setGongzuo(Bumen gongzuo){
    	this.gongzuo=gongzuo;
    }
    
    public int getTime(){
    	return time;
    }
    public void setTime(int time){
    	this.time=time;
    }
    public String toString(){
    	return "员工编号:"+this.getNum()+"         姓名:"+this.getMing()
                +"           性别:"+this.getSex()+"          参加工作时间:"+this.getTime() +"          生日:"+this.datatime+"   	      所在部门:"+this.gongzuo.getCheng()
              ;
    }
    }
    class Bumen{
    private String ber;
    private String cheng;
    private Zhi jingli;
    
    public Bumen(){
    	
    }
    public Bumen(String ber,String cheng){
    	this.ber=ber;
    	this.cheng=cheng;
    	
    }
    
    public String getBer(){
    	return ber;
    }
    public void setBer(String ber){
    	this.ber=ber;
    }
    public String getCheng(){
    	return cheng;
    }
    public void setCheng(String cheng){
    	this.cheng=cheng;
    }
    public Zhi getJingli(){
    	return jingli;
    }
    public void setJingli(Zhi jingli){
    	this.jingli=jingli;
    }
    }
    
    public class Test {
    public static void main(String args[]){
    	
    	  Bumen lai1=new Bumen("01","校外部");
    	  Bumen lai2=new Bumen("02","办公室");
    	 
    	  
    	  Zhi ren[] = new Zhi[10];
    	  ren[0]=new Zhi("001","和乔伊","女",new Days(1997,10,30),2015,lai1);
    	  ren[1]=new Zhi("002","赵绿","男",new Days(1995,03,29),2013,lai1);
    	  ren[2]=new Zhi("003","赵黑","男",new Days(1992,05,30),2015,lai2);
    	  ren[3]=new Zhi("004","任秀兴","男",new Days(2000,07,12),2011,lai2);
    	  ren[4]=new Zhi("005","张莹光","女",new Days(1996,02,26),2012,lai1);
    	  ren[5]=new Zhi("006","焦梦浩","男",new Days(1997,03,30),2014,lai2);
          ren[6]=new Zhi("007","陈树仁","女",new Days(1998,03,30),2009,lai1);
    	  ren[7]=new Zhi("008","刘玉","女",new Days(1992,05,02),2007,lai2);
    	  ren[8]=new Zhi("009","程菲菲","女",new Days(1993,03,16),2003,lai1);
    	  ren[9]=new Zhi("010","范一厚","男",new Days(1994,03,25),2011,lai2);
    	 
    	  lai1.setJingli(ren[0]);
    	  lai2.setJingli(ren[3]);
    	  
       for(int i=0;i<10;i++){
    		  System.out.println(ren[i].toString()+"        经理 ;"+ren[i].getGongzuo().getJingli().getMing());
        }
    }
    }
    

    程序设计思路:构造日期类,职工类,部门类,相互交叉使用。

    问题:测试类写的繁琐

    原因:没有真正理解构造类

    解决方案:认真翻阅课本,查阅资料

    2.设计一个计算工具类,要求:

    (1)为用户提供几个常见的几何体面积和体积的算法(如球的体积和表面积,圆柱体的体积和表面积,圆锥体的体积和表面积)(可以学习使用static关键字)

    (2)设计一个测试类,给出几个不同的圆柱体,圆锥体,球体的尺寸,用计算工具类分别求它们体积的累加和与表面积的累加和。

    (3)设计一个测试类,随机产生球,圆柱、圆锥体的尺寸,模拟考试,要求用户回答它们的体积和面积,并判断用户的回答是否正确

    程序

    class Yuanzhu{
    private double a;
    private double b;
    private double v;
    private double s;
    public Yuanzhu(){
    	
    }
    public Yuanzhu(double a,double b){
    	this.a=a;
    	this.b=b;
    	this.v=3.14*a*a*b;
    	this.s=3.14*a*a*2+a*2*3.14*b;
    }
    
    public String toString(){
    	return    "半径"+this.a+"       高"+this.b+"       体积"+this.v+"        表面积"+this.s;
    }
    public double getA(){
    	return a;
    }
    public void setA(double a){
    	this.a=a;
    }
    public double getB(){
    	return b;
    }
    public void setB(double b){
    	this.b=b;
    }
    public double getV(){
    	return v;
    }
    public void setV(double v){
    	this.v=3.14*a*a*b;
    }
    public double getS(){
    	return s;
    }
    public void setS(double s){
    	this.s=3.14*a*a*2+a*2*3.14*b;
    }
    }
    class Qiu{
    private double c;
    private double v;
    private double s;
    public Qiu(){
    	
    }
    public Qiu(double c){
    	this.c=c;
    	this.v=3.14*c*c*c*4/3;
    	this.s=3.14*4*c*c;
    }
    public String toString(){
    	return    "半径"+this.c+"       体积"+this.v+"        表面积"+this.s;
    }
    public double getC(){
    	return c;
    }
    public void setC(double c){
    	this.c=c;
    }
    public double getV(){
    	return v;
    }
    public void setV(double v){
    	this.v=3.14*c*c*c*4/3;
    }
    public double getS(){
    	return s;
    }
    public void setS(double s){
    	this.s=3.14*4*c*c;
    }
    }
    class Yuanzhui{
    private double m;
    private double n;
    private double r;
    private double l;
    private double h;
    private double v;
    private double s;
    public Yuanzhui(){
    	
    }
    public Yuanzhui(double n,double m,double r){
    	this.n=n;
    	this.m=m;
    	this.r=r;
    	this.l=(m*m)-(r*r);
    	this.h=java.lang.Math.sqrt(l);
    	this.s=(n/360)*m*m*3.14+3.14*r*r;
        this.v=3.14*r*r*h*1/3;
    }
    public String toString(){
    	return    "底面半径"+this.r+"       母线"+this.m+"       角度"+this.n+"       体积"+this.v+"        表面积"+this.s;
    }
    public double getN(){
    	return n;
    }
    public void setN(double n){
    	this.n=n;
    }
    public double getM(){
    	return m;
    }
    public void setM(double m){
    	this.m=m;
    }
    public double getR(){
    	return r;
    }
    public void setR(double r){
    	this.r=r;
    }
    public double getL(){
    	return l;
    }
    public void setL(double l){
    	this.l=l;
    }
    public double getH(){
    	return h;
    }
    public void setH(double h){
    	this.h=h;
    }
    public double getV(){
    	return v;
    }
    public void setV(double v){
    	this.v=1/3*3.14*r*r*h;
    }
    public double getS(){
    	return s;
    }
    public void setS(double s){
    	this.s=(n/360)*m*m*3.14+3.14*r*r;
    }
    }
    
    public class Test {
     public static void main(String args[]){
    	 Yuanzhu p1=new Yuanzhu(1,2);
    	 Yuanzhu p2=new Yuanzhu(3,2);
    	 System.out.println(p1.toString());
    	 
    	 Qiu q=new Qiu(3);
    	 System.out.println(q.toString());
    	 
    	 Yuanzhui o=new Yuanzhui(60,5,3);
    	 System.out.println(o.toString());
    	 
    	 
    	 double f=0;
    	 f=p1.getV()+p2.getV();
    	 System.out.println("体积之和为:"+f);
    	 
    	 double w=0;
    	 w=p1.getV()+q.getV()+o.getV();
    	 System.out.println("体积之和为:"+w);
    	 
    	 
    	 double u=0;
    	 u=p1.getS()+q.getS()+o.getS();
    	 System.out.println("表面积之和为:"+u);
     }
    }
    
    
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class Test2 {
    public static void main(String args[]){
    	int a,b,c;
    	Scanner input=new Scanner(System.in);
    	Random rand = new Random();
    	a=rand.nextInt(10);
    	b=rand.nextInt(10);
    	c=rand.nextInt(360);
    	
    	
    	Yuanzhu p=new Yuanzhu(a,b);
    	System.out.println("圆柱半径:"+a+"       高:"+b);
    	System.out.println("请求出表面积:");
    	int num1 = input.nextInt();
    	if(num1==p.getS()){
    		System.out.println("回答正确!");
    	}else{
    		System.out.println("回答错误!");
    	}
    	System.out.println("请求体积:");
    	int  num2= input.nextInt();
    	if(num2==p.getV()){
    		System.out.println("回答正确!");
    	}else{
    		System.out.println("回答错误!");
    	}
    	
    	
    	 Qiu q=new Qiu(a);
    	 System.out.println("球半径:"+a);
    	 System.out.println("请求出表面积:");
    	 int num3 = input.nextInt();
    		if(num3==q.getS()){
    			System.out.println("回答正确!");
    		}else{
    			System.out.println("回答错误!");
    		}
    	 System.out.println("请求体积:");
    		int  num4= input.nextInt();
    		if(num4==q.getV()){
    			System.out.println("回答正确!");
    		}else{
    			System.out.println("回答错误!");
    		}
    		
    		
    		Yuanzhui o=new Yuanzhui(c,a,b);
    		System.out.println("底面半径"+a+"       母线"+b+"       角度"+c);
    		 System.out.println("请求出表面积:");
    		 int num5 = input.nextInt();
    			if(num5==o.getS()){
    				System.out.println("回答正确!");
    			}else{
    				System.out.println("回答错误!");
    			}
    		 System.out.println("请求体积:");
    			int  num6= input.nextInt();
    			if(num6==o.getV()){
    				System.out.println("回答正确!");
    			}else{
    				System.out.println("回答错误!");
    			}
    }
    }
    

    程序设计思路:构造圆柱类,球类和圆锥类,在测试类里利用了随机数函数和输入函数,进行计算数值。

    作业链接

    (三)代码托管

  • 相关阅读:
    CentOS 7 配置 ISCSI 服务器
    CentOS 7 配置 http 服务器
    CentOS 7 配置 samba服务器
    ssh的两种连接方法(包括无密码访问)
    CentOS 7 破解mariadb密码
    Selenium2+python自动化53-unittest批量执行(discover)【转载】
    Selenium2+python自动化52-unittest执行顺序【转载】
    Selenium2+python自动化51-unittest简介【转载】
    selenium3+python自动化50-环境搭建(firefox)【转载】
    Selenium2+python自动化49-判断文本(text_to_be_present_in_element)【转载】
  • 原文地址:https://www.cnblogs.com/renxiuxing/p/6675864.html
Copyright © 2011-2022 走看看