201871010116- Qi British Red "object-oriented programming (java)" seventh week learning summary

project

content

"Object-oriented programming (java)"

https://www.cnblogs.com/nwnu-daizh/

Where this requirement in the job

https://www.cnblogs.com/nwnu-daizh/p/11654436.html

Job learning objectives

  1. Master the use of the characteristics of four access rights modifier;
  2. Object class master and common use of the API;
  3. ArrayList control methods and uses defined class;
  4. Master enumeration class defines methods and uses;
  5. Chapter binding experiments, inheritance and polymorphism understood that two OOP features, and appreciate its advantages.

 

Experimental details and steps

Experiment 1: (20 minutes)

Code is as follows:

class Parent { 
	Private String P1 = "This is a Parent private attribute"; 
	public String P2 = "This is a Parent public properties"; 
	protected String P3 = "This is the Parent Protected property"; 
	String P4 = "This is a Parent default properties "; 
	private void pMethod1 () { 
		System.out.println (" Parent I used a modified method of the private modifier "); 
	} 
	public void pMethod2 () { 
		System.out.println (" I use public Parent method modified modifiers "); 
	} 
	protected void pMethod3 () { 
		System.out.println (" I Parent protected modifier with the modified method "); 
	} 
	void pMethod4 () { 
		System.out.println (" I Parent modifier without modification method "); 
	} 
} 
class Parent {Son the extends 
	private String S1 =" this is a private property Son ";
	public String s2 = "attribute which is public Son";
	protected String s3 = "This is a protected attribute Son"; 
	String S4 = "This is the default attribute of the Son"; 
	public void sMethod1 () { 
		System.out.println (); // try p1 Parent category are displayed, p2, p3, p4 value 
		System.out.println ( "I Son modified method with a public modifier"); 
	} 
	private void sMethod2 () { 
		System.out.println ( "I Son modification method using the private modifier "); 
	} 
	protected void sMethod () { 
		System.out.println (" I Son modified method using protected modifier "); 
	} 
	void sMethod4 () { 
		System.out.println (" I am no modifier for modifying Son method "); 
	}	 
} 
public class Demo { 
	public static void main (String [] args) { 
		the parent = parent new new the parent (); 
		Son Son = Son new new (); 
		System.out.println (); // call the method with the parent class, respectively Paren attempt, Son method call class with the son	 
	} 
}  

Since this class is only visible private property, public property visible to all classes, protected attributes of the package and visible to all classes, the default property of the package is visible.

The p1 Parent class is Parent of private property, can not be called in a subclass, so the program error, can not be displayed; p2 is the Parent of public property, p3 is the Parent protected property, p4 is the default property Parent of these properties can be invoked in the subclass, it can display the value p2, p3, p4 of.

Run process is shown:

Ibid. parent classes and class son of a private property can not be called, so call p1 and s1 running error.

The method of operation with the result of the call parent class Paren follows:

 

 

 

The method of operation with the result of the call class son Son follows:

 

 

 

Experiment 2: Test Procedure 1 (15 minutes)

 5-8 code is as follows:

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());
   }
}

5-9程序代码如下:

package equals;

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

public class Employee
{
   private String name;//private定义了一个只能在该类中访问的字符串变量
   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)
   {
      // a quick test to see if the objects are identical  快速测试这些对象是否相同
      if (this == otherObject) return true;

      // must return false if the explicit parameter is null   如果显示参数为空,必须返回false
      if (otherObject == null) return false;

      // if the classes don't match, they can't be equal   如果第几个类不匹配,则他们不相同
      if (getClass() != otherObject.getClass()) return false;

      // now we know otherObject is a non-null Employee   //其他对象为非空Employee类
      var other = (Employee) otherObject;

      // test whether the fields have identical values   //测试是不是有相同值
      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 + "]";
   }
}

5-10程序代码如下:

package equals;

public class Manager extends Employee  //扩展了一个子类Manager
{
   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;
   }

   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  用super.equals检查这个类和其他类是否属于同一个类
      return bonus == other.bonus;
   }

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

   public String toString()//吧其他类型的数据转换为字符串类型的数据
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }
}

5-8、5-9、5-10程序运行结果如下:

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

equals方法:用于检测一个对象是否等于另外一个对象,在子类中调用equals方法时,首先调用超类的equals,如果检测失败,对象就不可能相等,如果超类中的域都相等,就需要比较子类中的实例域。

hashCode方法:散列码(hash code)是由对象导出的一个整型值,且没有规律,由于hashCode方法定义在object类中,因此每个对象都有一个默认的散列码,其值是对象的存储地址该方法返回一个整型数值。

toString方法:用于返回表示对象值的字符串,只要对象与一个字符串通过操作符“+”连接起来,Java编译就会自动地调用toString方法。

 

 Employee类重写后代码如下:

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;
   }
 
@Override
public int hashCode() {   //重写hashCode方法,使相等的两个对象获取的HashCode也相等
    // TODO Auto-generated method stub
    return Objects.hash(name, salary, hireDay);
}
 
@Override
public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    if (this == obj) return true;     //快速测试几个类的根是否相同,即是否是同一个超类。这个if语句判断两个引用是否是同一个,如果是同一个,那么这两个对象肯定相等。
    if (obj == null) return false;   //如果显示参数为空,则返回false
    if (getClass() !=obj.getClass()) return false;   //用getClass()方法得到对象的类。如果几个类不匹配,则它们不相等
 
    //其他对象是非空Employee类
    //在以上判断完成,再将得到的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,然后再判断对象的属性是否相同
    Employee other = (Employee) obj;
    //测试字段是否具有相同的值
    return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
 }
 
 
@Override
public String toString() {  //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)
    // TODO Auto-generated method stub
    return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
 
}

Manager类重写之后代码如下:

package equals;
 
public class Manager extends Employee     //子类:Manager类继承Employee类
{
   private double bonus;     //创建私有属性bouns
 
   public Manager(String name, double salary, int year, int month, int day)
   {
      super(name, salary, year, month, day);    //子类直接调用超类中已创建的属性
      bonus = 0;      //给bouns赋初值为空
   }
 
   public double getSalary()//访问器
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }
 
   public void setBonus(double bonus)   //更改器
   {
      this.bonus = bonus;
   }
 
   public boolean equals(Object otherObject)   //快速测试几个类的根是否相同,即是否是同一个超类
   {
      if (!super.equals(otherObject)) return false;
      Manager other = (Manager) otherObject;
       //使用super.equals检查这个类和其他是否属于同一个类
      return bonus == other.bonus;
   }
 
   public int hashCode()    //重写hashCode方法,使相等的两个对象获取的HashCode也相等
   {
      return java.util.Objects.hash(super.hashCode(), bonus);
   }
 
   public String toString()    //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }
}

EmployeeTest类代码如下:

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)
   {
      Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      Employee alice2 = alice1;
      Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      Employee 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);
 
      Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      Manager 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());
   }
}

  程序运行结果如下图:

  

实验2:测试程序2(15分)

 5-11程序代码如下:

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)
   {
      // fill the staff array list with three Employee objects  用三个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));

      // raise everyone's salary by 5%   将每个人的薪水提高5%
      for (Employee e : staff)
         e.raiseSalary(5);

      // print out information about all Employee objects   打印出所有Employee类的信息
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
            + e.getHireDay());
   }
}

  

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;
   }//定义两个局部变量
}

  程序运行结果如下:

 设计适当的代码,测试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
{
   private static final Employee element = null;
   private static final int index = 0;
 
   public static void main(String[] args)
   {
      // fill the staff array list with three Employee objects
      ArrayList<Employee> staff = new ArrayList<Employee>();   //用三个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));
      ArrayList<Employee> list = new ArrayList<Employee>();
       
    //size()的用法
      int size=staff.size();
      System.out.println("arrayList中的元素个数是:"+size);
      for(int i=0;i<staff.size();i++)
      {
          //get()的用法
          Employee e=staff.get(i);
          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                    + e.getHireDay());
      }
      //set()的用法
      staff.set(0, new Employee("llx", 20000, 1999, 11, 06));
      Employee e=staff.get(0);
      System.out.println("修改后的数据为:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                + e.getHireDay());
       
      //remove()的用法
      staff.remove(2);
      System.out.println("将第一个数据删除后:");
      int size1=staff.size();
      System.out.println("arrayList中的元素个数是:"+size1);
      for(int i=0;i<staff.size();i++)
      {
          Employee p=staff.get(i);
          System.out.println("name=" + p.getName() + ",salary=" + p.getSalary() + ",hireDay="
                    + p.getHireDay());
      }
      
      // raise everyone's salary by 5%
      for (Employee e1 : staff)    //把每个人的薪资提高%5
         e1.raiseSalary(5);
 
      // print out information about all Employee objects
      for (Employee e1 : staff)   //输出所有雇员对象的信息
         System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay="
            + e1.getHireDay());    //利用getName(),getSalary() 和getHireDay()方法输出所有雇员对象的信息
   }
}

  程序运行结果如图:

实验2:测试程序3(15分)

程序代码如下:

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);  //静态values方法返回枚举的所有值的数组
      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;
}//调用构造函数

  程序运行结果如下:

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

 所有的枚举类型都是Enum类地子类,他们继承了这个类的许多方法,其中最有用的是toString,这个方法能够返回枚举常量名。每个枚举类型都有一个静态的values方法,它将返回一个包含全部枚举值的数组。ordinal方法返回enum声明中枚举常量的位置,位置从0开始计数。 

删除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);  //静态values方法返回枚举的所有值的数组
      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;
}//调用构造函数

 程序运行结果如下:

实验2:测试程序4(5分)

程序代码如下:

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);  
    }  
}

  程序运行结果如下:

实验3:编程练习(10分)

 程序代码如下:

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 {
	//补全本类定义
	Son() {
	super(true);    //调用父类有参构造
	    System.out.println("Son's Constructor without parameter");
	}
	public void method() {
	    System.out.println("Son's method()");
	    super.method();
	}
}

  程序运行结果如下:

3. 实验总结:(15分)

 通过学习继承这一章的内容以及实验,掌握了继承建立类与类间的is-a关系,意味着父类对象变量可以引用子类对象,在Java中,一个父类可以有多个子类,但一个子类只能有一个父类,子类通过extends关键字来继承父类,父类和子类的三种继承方式:1.public继承,(public成员所有的类都可以访问);2.private继承,(private成员只有本类能访问);3.protect继承,(protect成员只有本类和子类能访问)。子类继承父类是对父类属性和方法的全面继承,同时子类在继承了父类的方法后可对父类的方法进行重写。父类用作对象的声明类型,构造器用子类的构造器。object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的,如果没有明确的指出超类,object就被认为是这个类的超类,

Guess you like

Origin www.cnblogs.com/qyhq/p/11666487.html