201871010104- Chen Yuanyuan "object-oriented programming (java)" eighth week learning summary

                                                                                                             201871010104- Chen Yuanyuan "object-oriented programming (java)" eighth week learning summary

project
content
This work belongs courses https://www.cnblogs.com/nwnu-daizh/
Where the job requires https://www.cnblogs.com/lily-2018/p/11441372.html
Job learning objectives

(1) Method master interface definition;

(2) master interface class implementation-defined requirements;

(3) master enables the use requirements of the interface class;

(4) callback master design patterns;

(5) master Comparator interface usage;

(6) grasp the object shallow and deep copy copying method;

(7) master Lambda expression syntax;

(8) The use and learn the syntax required within the class.

Part I: summary of theoretical knowledge

 

Abstract class:

 

       With abstract to declare, there is no specific instance of the class object, can not be used to create new objects. It may comprise any conventional class contains things. Abstract class must be inherited by subclasses, if abstract subclass is not abstract class, subclass of all abstract methods in the parent class must be rewritten.

 

interface:

 

       Interface with the statement, is a collection of abstract methods defined and constant values. Essentially, the interface is a special abstract class, which defines only the abstract class contains constants and methods, but does not define the variables and methods. Interface can only define abstract methods, and these methods are public by default. As long as the class implements the interface, you can use objects of this class wherever they are needed for the interface. In addition, a class can implement multiple interfaces.

 

The difference between abstract classes and interfaces:

 

(1) the interface can not be achieved by any method, and may be an abstract class.

 

(2) class can implement many interfaces, but only one parent.

 

Part (3) of the interface is not a class hierarchy, without any contact of the class can implement the same interface.

The method defined in the interface: In the Java programming language, the interface is not a class, but the class a set of requirements is described by a set of constants and abstract methods composition. Interface does not include variables and methods particular implementation. As long as the class implements the interface, the class to follow a uniform format interface descriptions are defined, and you can use objects of this class wherever they are needed for the interface.

Implementation of the interface: use with implements keyword class declaration declares one or more interfaces, a class uses an interface, then the class must implement all interface methods, these methods shall provide a method body.
 A class can implement multiple interfaces, between the interface should be separated by commas.

Interface requirements: Interface interface object can not be constructed, but the interface can declare a variable to point to the object class that implements the interface.

 

Callback (callback):

 

       A programming mode, in this mode, you can point out the action when a specific event in the program should take. When copying an object variable, and copy the original variable variables reference the same object. In this way, the other variable will change the object referenced by a variable impact. If you want to create a new object of copy, its original state as the original, but you can later change their status, we need to use the clone method of the Object class. clone Object class () method is a native method. clone Object class () method is modified protected modifier. This means you can not call it directly in the user-written code. If the attribute is applied directly clone () method, on the need to cover clone () method, and should clone () method set to public. The Object.clone () method returns an Object object. You must be cast to get the required type.

There is a Timer class, it can be used to trigger an event when arriving in a given time interval java.swing package.

 

Shallow copy:

 

        The object is copied to all members of the constants and basic types of properties have the same value as the original copy of the object, and if an object is a member of the domain, were copies of the object to the domain object reference still points to the original object.

 

Deep copy:

 

       All members of the domain is copied objects contain the same value as the original target, and the target domain will point to the new object is copied, not the object of the original object being referenced. In other words, the deep copy to copy objects referenced within the object be copied again.

 

Java objects clone implementation:

 

       Cloneable interface implemented in a subclass. To obtain a copy of the object, a method may be utilized clone Object class. Override the clone method superclass in a subclass, declared as public. In the clone method subclass, call super.clone ().

 

Part II: Experimental part

Experiment 1 :  introducing Chapter 6 sample programs, test procedures and code comments.

Test Procedure 1:

1) editing, compiling, debugging and running Read textbook pages 214-215 page program 6-1, 6-2, to understand the program and analyze the result of the program;

2) add a comment new knowledge at the relevant code in the program.

3) implement the use of master interface; mastered the use of built-in interfaces Compareable.

code show as below:

EmployeeSortTest class
the interfaces Package; 

Import Classes in java.util *;. 

/ ** 
 . * This Program The Demonstrates The use of the Comparable interface 
 * @version 1.30 2004-02-27 
 * @author Cay Horstmann 
 * / 
public class EmployeeSortTest 
{ 
   public static void main (String [] args) 
   { 
	  the employee [] = new new Staff the employee [. 3]; // create an array of three employee 

      Staff [0] = new new the employee ( "Harry Hacker", 35000); 
      Staff [. 1] = the employee new new ( "Carl Cracker ", 75000); 
      Staff [2] = new new Employee (" Tony Tester ", 38000); 

      Arrays.sort (Staff); // arrays of the sort of class Employee object sorting array (only static methods modified and it has to be called with) 

     // print out all the information Employee objects 
      for (Employee e: staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
   }
}
 Employee class
package interfaces;

public class Employee implements Comparable<Employee>//实现接口类
{
   private String name;
   private double salary;

   public Employee(String name, double salary)
   {
      this.name = name;
      this.salary = salary;
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

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

   /**
    * Compares employees by salary
    * @param other another Employee object
    * @return a negative value if this employee has a lower salary than
    * otherObject, 0 if the salaries are the same, a positive value otherwise
    */
   public int compareTo(Employee other)
   {
      return Double.compare(salary, other.salary);//将雇员的salary进行比较
   }
}

 Results are as follows:

Test Procedure 2:

Edit, compile, debug the program, run in conjunction with the results of the program to understand the program;

interface  A
{
  double g=9.8;
  void show( );
}
class C implements A
{
  public void show( )
  {System.out.println("g="+g);}
}

class InterfaceTest
{
  public static void main(String[ ] args)
  {
       A a=new C( );
       a.show( );
       System.out.println("g="+C.g);
  }
}

Results are as follows:

 

 Test Procedure 3 :

1) In the elipse IDE commissioning textbook 223 this page 6-3 , in conjunction with the results of running the program understanding;

2) 26 lines, 36 lines see 224 page details concerning materials 12 Cap.

3) add a comment new knowledge at the relevant code in the program. Callback master programming mode;

code show as below:

package timer;

/**
   @version 1.02 2017-12-14
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.util.Date;

import javax.swing.*;

public class TimerTest
{  
   public static void main(String[] args)
   {  
      ActionListener listener = new TimePrinter();
        //构造一个定时器为listener
            //10秒一次
      Timer t = new Timer(1000, listener);
      t.start();

      // keep program running until the user selects "OK"
      JOptionPane.showMessageDialog(null, "Quit program?");
      System.exit(0);
   }
}

class TimePrinter implements ActionListener
{  
   public void actionPerformed(ActionEvent event)
   {  
      System.out.println("At the tone, the time is " 
         + new Date());
      Toolkit.getDefaultToolkit().beep();
   }
}

 Results are as follows:

 Test Procedure 4 :

1) commissioning textbook 229 pages -231 page program 6-4 , 6-5 , combined result of the program understand the program;

2) add a comment new knowledge at the relevant code in the program.

3) grasp the object cloning techniques to achieve;

4) master shallow vs. deep copy difference.

code show as below:

 CloneTest class

clone Package; 

/ ** 
 . * This Program Demonstrates Cloning 
 * @version 1.11 2018-03-16 
 * @author Cay Horstmann 
 * / 
public class CloneTest 
{ 
   public static void main (String [] args) throws CloneNotSupportedException 
   { 
	   the try {// the try clause of an exception is likely to code 
	  the Employee = new new Original the Employee ( "John Q. Public", 50000); 
      original.setHireDay (2000,. 1,. 1); 
      the Employee original.clone = Copy (); 
      copy.raiseSalary (10); 
      copy.setHireDay (2002, 12 is, 31 is); 
      System.out.println ( "Original =" Original +); 
      System.out.println ( "= Copy" Copy +);
   }
	   catch (CloneNotSupportedException e) // not implemented cloneable interface throws an exception
	   { 
		   E.printStackTrace (); 
	   } 
   } 
}

  Employee class

package clone;

import java.util.Date;
import java.util.GregorianCalendar;

public class Employee implements Cloneable//创建深拷贝
{
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String name, double salary)
   {
      this.name = name;
      this.salary = salary;
      hireDay = new Date();
   }

   public Employee clone() throws CloneNotSupportedException
   {
	 
	 //调用对象克隆
      Employee cloned = (Employee) super.clone();

     //克隆易变字段
      cloned.hireDay = (Date) hireDay.clone();

      return cloned;
   }

   /**
    * Set the hire day to a given date. 
    * @param year the year of the hire day
    * @param month the month of the hire day
    * @param day the day of the hire day
    */
   public void setHireDay(int year, int month, int day)
   {
      Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
      
      // example of instance field mutation
      hireDay.setTime(newHireDay.getTime());
   }

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

   public String toString()
   {
      return "Employee[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
   }
}

Results are as follows:

Difference between deep copy and shallow copy

Shallow copy: the object is copied to all members of the constants and basic types of properties have the same value as the original copy of the object, and if an object is a member of the domain, were the object of the object to copy domain still points to the original object.

Deep copy: All members of the domain is copied objects contain the same value as the original target, and the target domain will point to the new object is copied, not the object of the original object being referenced. In other words, the deep copy to copy objects referenced within the object be copied again.

 Experiment 2 : introducing a first 6 Zhang example program 6- 6 , learn learning Lambda Expression usage. 

1) commissioning textbook 233 pages -234 page program 6-6 , combined result of the program understand the program;

2) add a comment new knowledge at the relevant code in the program.

3) the 27-29 line Textbook 223 Comparative page program, the 27-29 line with this procedure contrast, experience Lambda advantages expression.

code show as below:

 

 

  

  

 

 

  

 

interface  A

{

  double g=9.8;

  void show( );

}

class C implements A

{

  public void show( )

  {System.out.println("g="+g);}

}

 

class InterfaceTest

{

  public static void main(String[ ] args)

  {

       A a=new C( );

       a.show( );

       System.out.println("g="+C.g);

  }

}

实验总结:

在做实验时,有学长以及老师的讲解,是我对接口的知识有了更深一层的理解,以及在后面的自主练习的实验中,虽然对实验的认知还不太透彻,希望在后期的学习中能加强吧。

Guess you like

Origin www.cnblogs.com/chanyeol1127/p/11697560.html