201871010111-刘佳华《面向对象程序设计(java)》第七周学习总结


201871010111-刘佳华《面向对象程序设计(java)》第七周学习总结

实验时间 2019-10-11

1、实验目的与要求

1) 掌握四种访问权限修饰符的使用特点;

(1)进一步理解4个成员访问权限修饰符的用途;

A.仅对本类可见-private

B.对所有类可见-public

C.对本包和所有子类可见-protected

D.对本包可见-默认,,不需要修饰符

2) 掌握Object类的用途及常用API;

3) 掌握ArrayList类的定义方法及用法;

4)掌握枚举类定义方法及用途;

5)结合本章实验内容,理解继承与多态性两个面向对象程序设计特征,并体会其优点。

2、实验内容和步骤

实验1 System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法。

class Parent {

private String p1 = "这是Parent的私有属性";

public String p2 = "这是Parent的公有属性";

protected String p3 = "这是Parent受保护的属性";

String p4 = "这是Parent的默认属性";

private void pMethod1() {

System.out.println("我是Parentprivate修饰符修饰的方法");

}

public void pMethod2() {

System.out.println("我是Parentpublic修饰符修饰的方法");

}

protected void pMethod3() {

System.out.println("我是Parentprotected修饰符修饰的方法");

}

void pMethod4() {

System.out.println("我是Parent无修饰符修饰的方法");

}

}

class Son extends Parent{

private String s1 = "这是Son的私有属性";

public String s2 = "这是Son的公有属性";

protected String s3 = "这是Son受保护的属性";

String s4 = "这是Son的默认属性";

public void sMethod1() {

System.out.println(...);//分别尝试显示Parent类的p1、p2、p3、p4值

System.out.println("我是Sonpublic修饰符修饰的方法");

}

private void sMethod2() {

System.out.println("我是Sonprivate修饰符修饰的方法");

}

protected void sMethod() {

System.out.println("我是Sonprotected修饰符修饰的方法");

}

void sMethod4() {

System.out.println("我是Son无修饰符修饰的方法");

}

}

public class Demo {

public static void main(String[] args) {

Parent parent=new Parent();

Son son=new Son();

System.out.println(...); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法

}

}

 

public class Demo {

    public static void main(String[] args) {
    Parent parent=new Parent();
    Son son=new Son();
    //System.out.println(...); //分别尝试用parent调用Parent类的方法、访问值;
     parent.pMethod2();
     parent.pMethod3();
     parent.pMethod4();//由于pMethod1为私有的,所以不能用parent.pMethod1调用
     System.out.println(parent.p2);
     System.out.println(parent.p3);
     System.out.println(parent.p4);
     System.out.println("**********************************");
     //用son调用Son类的方法
     son.sMethod1();
     son.sMethod3();
     son.sMethod4();
     System.out.println("**********************************");
     //用son调用parent类的方法
     son.pMethod2();
     System.out.println("**********************************");
    }

}
Demo
package SY1;

public class Parent {
    private String p1 = "这是Parent的私有属性";
    public String p2 = "这是Parent的公有属性";
    protected String p3 = "这是Parent受保护的属性";
    String p4 = "这是Parent的默认属性";
    private void pMethod1() {
     System.out.println("我是Parent用private修饰符修饰的方法");
    }
    public void pMethod2() {
     System.out.println("我是Parent用public修饰符修饰的方法");
    }
    protected void pMethod3() {
     System.out.println("我是Parent用protected修饰符修饰的方法");
    }
    void pMethod4() {
     System.out.println("我是Parent无修饰符修饰的方法");
    }
 }
Parent
package SY1;

Public class Son extends Parent{
        private String s1 = "这是Son的私有属性";
        public String s2 = "这是Son的公有属性";
        protected String s3 = "这是Son受保护的属性";
        String s4 = "这是Son的默认属性";
        public void sMethod1() {
         System.out.println(p2);//分别尝试显示Parent类的p1、p2、p3、p4值
         System.out.println("我是Son用public修饰符修饰的方法");
        }
        private void sMethod2() {
         System.out.println("我是Son用private修饰符修饰的方法");
        }
        protected void sMethod3() {
         System.out.println("我是Son用protected修饰符修饰的方法");
        }
        void sMethod4() {
         System.out.println("我是Son无修饰符修饰的方法");
        }
 }
Son

运行截图:

 @@当新建一个以LJH的包,把Parent.java文件移动到LJH的包中时,通过在Damo.java 中加入import LJH.Parent;

但是由于Damo.java和Parent.java 不在同一个包中,一些属性及方法将在访问时产生错误,这也让我深刻认识到了权限修饰符的作用;如下图:

                        

 

实验2:导入第5章以下示例程序,测试并进行代码注释。

测试程序1:

l 运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);

删除程序中Employee类、Manager类中的equals()hasCode()toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。

package equals;

import java.time.*;
import java.util.Objects;

public class Employee
{
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }

   public boolean equals(Object otherObject)
   {
       // 快速测试,看看这些对象是否相同
      if (this == otherObject) return true;

       // 如果显式参数为空,则必须返回false
      if (otherObject == null) return false;

       // 如果类不匹配,它们就不能相等
      if (getClass() != otherObject.getClass()) return false;

      // 现在我们知道otherObject是一个非空雇员
      var other = (Employee) otherObject;

      // 测试字段是否具有相同的值
      return Objects.equals(name, other.name) 
         && salary == other.salary && Objects.equals(hireDay, other.hireDay);
   }

   public int hashCode()
   {
      return Objects.hash(name, salary, hireDay); 
   }

   public String toString()
   {
      return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" 
         + hireDay + "]";
   }
}
Employee
package equals;

/**
 * This program demonstrates the equals method.
 * @version 1.12 2012-01-26
 * @author Cay Horstmann
 */
public class EqualsTest
{
   public static void main(String[] args)
   {
      var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      var alice2 = alice1;
      var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

      System.out.println("alice1 == alice2: " + (alice1 == alice2));

      System.out.println("alice1 == alice3: " + (alice1 == alice3));

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));

      System.out.println("bob.toString(): " + bob);

      var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      boss.setBonus(5000);
      System.out.println("boss.toString(): " + boss);
      System.out.println("carl.equals(boss): " + carl.equals(boss));
      System.out.println("alice1.hashCode(): " + alice1.hashCode());
      System.out.println("alice3.hashCode(): " + alice3.hashCode());
      System.out.println("bob.hashCode(): " + bob.hashCode());
      System.out.println("carl.hashCode(): " + carl.hashCode());
   }
}
EqualsTest
package equals;

public class Manager extends Employee
{
   private double bonus;

   public Manager(String name, double salary, int year, int month, int day)
   {
      super(name, salary, year, month, day);
      bonus = 0;
   }

   public double getSalary()
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }

   public void setBonus(double bonus)
   {
      this.bonus = bonus;
   }

@Override
public boolean equals(Object otherObject) {
    // TODO Auto-generated method stub
    if (!super.equals(otherObject)) return false;
    var other = (Manager) otherObject;
     // super.equals检查这个和其他属于同一个类
    return bonus == other.bonus;
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    return super.hashCode();
}

@Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString();
}

   /*public boolean equals(Object otherObject)
   {
      if (!super.equals(otherObject)) return false;
      var other = (Manager) otherObject;
      // super.equals checked that this and other belong to the same class
      return bonus == other.bonus;
   }

   public int hashCode()
   {
      return java.util.Objects.hash(super.hashCode(), bonus);
   }

   public String toString()
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }*/
}
Manager

运行结果:

测试程序2:

l 在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;

l 掌握ArrayList类的定义及用法;

l 在程序中相关代码处添加新知识的注释;

l 设计适当的代码,测试ArrayList类的set()get()remove()size()等方法的用法。

package arrayList;

import java.util.*;

/**
 * This program demonstrates the ArrayList class.
 * @version 1.11 2012-01-26
 * @author Cay Horstmann
 */
public class ArrayListTest
{
   public static void main(String[] args)
   {
      // 用三个Employee对象填充staff数组列表
      var staff = new ArrayList<Employee>();

      staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
      staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
      staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));

      // 把每个人的薪水提高5%
      for (Employee e : staff)
         e.raiseSalary(5);

      // 打印所有Employee对象的信息
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
            + e.getHireDay());
   }
}
ArrayListTest
package arrayList;

import java.time.*;

public class Employee
{
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}
Employee

运行结果:

更改后的ArrayListTest代码如下:

 1 package arrayList;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * This program demonstrates the ArrayList class.
 7  * @version 1.11 2012-01-26
 8  * @author Cay Horstmann
 9  */
10 public class ArrayListTest
11 {
12    public static void main(String[] args)
13    {
14       // 用三个Employee对象填充staff数组列表
15       var staff = new ArrayList<Employee>();
16 
17       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
18       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
19       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
20       
21       System.out.println(staff.get(1));
22       staff.set(1, new Employee("liujiahua", 50000, 2019, 10, 11));
23       System.out.println(staff.size());
24       staff.remove(2);
25 
26       // 把每个人的薪水提高5%
27       for (Employee e : staff)
28          e.raiseSalary(5);
29 
30       // 打印所有Employee对象的信息
31       for (Employee e : staff)
32          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
33             + e.getHireDay());
34    }
35 }
new ArrayListTest

 

运行代码之后截图:

  可以看到,在修改代码之后运行可以看出,通过get()方法获得下标为1的元素储存位置,即地址通过;set()方法,修改在ArrayList<>中下标1添加new Employee("liujiahua", 50000, 2019, 10, 11);

通过get()方法获得数组下标为1的元素输出、通过remove()方法删除下标为2的数组元素、并且通过size()方法代替.length得到数组的长度。

 

测试程序3:

l 编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;

l 掌握枚举类的定义及用法;

l 在程序中相关代码处添加新知识的注释;

l 删除程序中Size枚举类,背录删除代码,在代码录入中掌握枚举类的定义要求。

package enums;

import java.util.*;

/**
 * This program demonstrates enumerated types.
 * @version 1.0 2004-05-24
 * @author Cay Horstmann
 */
public class EnumTest
{  
   public static void main(String[] args)
   {  
      var in = new Scanner(System.in);
      System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
      String input = in.next().toUpperCase();
      Size size = Enum.valueOf(Size.class, input);
      System.out.println("size=" + size);
      System.out.println("abbreviation=" + size.getAbbreviation());
      if (size == Size.EXTRA_LARGE)
         System.out.println("Good job--you paid attention to the _.");      
   }
}

enum Size
{
   SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

   private Size(String abbreviation) { this.abbreviation = abbreviation; }
   public String getAbbreviation() { return abbreviation; }
    
   private String abbreviation;
    
}
EnumTest

 运行结果:

测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法

public class TestVarArgus {  

    public static void dealArray(int... intArray){  

        for (int i : intArray)  

            System.out.print(i +" ");  

          

        System.out.println();  

    }        

    public static void main(String args[]){  

        dealArray();  

        dealArray(1);  

        dealArray(1, 2, 3);

   dealArray(1, 2, 3, 4); 

     dealArray(1, 2, 3, 4, 5); 

     dealArray(1, 2, 3, 4, 5, 6); 

    }  

}

运行截图:

实验:3:编程练习:参照输出样例补全程序,使程序输出结果与输出样例一致。

public class Demo {

public static void main(String[] args) {

Son son = new Son();

son.method();

}

}

class Parent {

Parent() {

System.out.println("Parent's Constructor without parameter");

}

Parent(boolean b) {

System.out.println("Parent's Constructor with a boolean parameter");

}

public void method() {

System.out.println("Parent's method()");

}

}

class Son extends Parent {

//补全本类定义

}

程序运行结果如下:

Parent's Constructor with a boolean parameter

Son's Constructor without parameter

Son's method()

Parent's method()

代码如下:

package LJH;

public class Demo {
public static void main(String[] args) {
    Son son = new Son();
    son.method();
}
}
class Parent {
        Parent() {
        System.out.println("Parent's Constructor without parameter");
        }
        Parent(boolean b) {
        System.out.println("Parent's Constructor with a boolean parameter");
        }
        public void method() {
        System.out.println("Parent's method()");
        }
    }
class Son extends Parent {
    void Parent() {
        System.out.println("Son's Constructor without parameter");
        }
       public void method() {
           Parent();
           System.out.println("Son's method()");
           super.method();
           
       }
//补全本类定义
    

}
/*
 Parent's Constructor with a boolean parameter
 Son's Constructor without parameter
 Son's method()
 Parent's method()*/
Demo

 

 运行结果:

 

实验总结:

通过本周的实验,我掌握理解了成员访问权限的四个修饰符,Object类和ArrayList类的常用方法,API以及枚举使用方法。在处理一些问题中,我了解到了我的不足,编程能力还远远不行。在继承学习中,仍有一些父类和子类的关系没搞懂;继承程序构造技术还不太熟练,需要继续学习巩固。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM