201871010111- Liu Jiahua "object-oriented programming (java)" fourth week of learning summary

201871010111- Liu Jiahua "object-oriented programming (java)" fourth week of learning summary

Experimental time 201 . 9 -9- 20 is

Part I: Chapter IV summarizes theoretical knowledge

4.1: the concept of classes and objects.

Class: Class is a template object construction. A process referred to as an object class configured to create an instance of the class.

Object: To use oop, be sure to clear three characteristics of the object:

Behavior 1) object: the behavior of the object using the defined methods can be invoked.

State-2) objects: Each object holds information describing the current feature.

3) object identifier: How to identify similarities have the same behavior.

4.2: The relationship between classes.

 Common relations are: dependence ( "uses-a"), the polymerization ( "has-a ''), inherited (" is-a ").

4.3: Use predefined classes.

We have learned the predefined classes such as math, Math, String, Scanner and so on.

1) objects and object variables.

In the Java language, using the constructor construct a new instance. Class constructor is a special method, to generate and initialize the object, its class name and method name of the same .

We want to construct a Data object (defined in the java.util), the need to add new operator before the constructor: new Data ()

 If defined as Data deadline; the statement is wrong.

An object variable may be set to null, indicating that the object variable does not refer to any variable, such as deadlin = null.

4.4: Changing with the accessor.

Change: Edit instance fields. Prefix set, change the current class property.

Access control: Change the instance fields. Prefix get.

4.4: user-defined classes;

4.5: static fields, and static methods;

4.6: method parameters; object constructor

4.7: Package

4.8: classpath

4.9: Documentation Comments

4.10: Class Design Tips

Part II: Experimental part

Experiment name: the definition and use of three types of experiments with the object

1. Purpose:

(1) familiar with the PTA platform for online testing environment;

(2) The user-defined appreciated class;

(3) grasp the object declaration;

(4) to learn to use a constructor initializes the object;

(5) using the class attributes and methods of using the master;

(6) to master the use of package and import statements.

2. Experimental details and steps

Experiment 1  using a personal account login https://pintia.cn/ , using a binding code 620 781 to join PTA platform NWNU-2019CST1 classes (Northwest Normal University Computer Science and Engineering 2018 level computer science and technology), to complete the " 2019 Fall northwest Normal programming object-oriented programming capability test 1 ", the test time of 50 minutes.

Experiment 2  introduced in Chapter 4 and test sample program.

Test Procedure 1 :

l editing, compiling, debugging and running programs 4-2 (Textbook 104);

l combined result of the program, to master the use of the class definition and class objects, and annotate the class with the object of knowledge application in the program code;

l try to edit two types of documents (Employee.java, EmployeeTest.java), compile and run the program in the project.

l reference materials 104 EmployeeTest.java, design StudentTest.java, define the Student class that contains the name (name), sex (gender), javascore (java performance) three fields, write a program, the number of students from the keyboard input, input student information , according to the following student information table header output:

  Name    Sex java results

Case textbooks compiled results are as follows:

  • The textbook program as a program code adapted:
import java.util.Scanner;
/**
 * This program tests the Student class.
 * time 2019 9 21
 * @author liu jiahua
 */
public class Main {

        public static void main(String[] args)
           {
            Student []stu=new Student[2];
            System.out.println("please input name,sex,score");
            Scanner in =new Scanner(System.in);
            
            for (int i=0;i<stu.length;i++){
                stu[i]=new Student(in.next(),in.next(),in.nextFloat());
            }
             System.out.println("name"+" "+"sex"+" "+" "+"javascore");
             
             for (Student o : stu)
                 System.out.println(o.getName() +"   "+o.getSex()+"        "+o.getJavaScore());
             in.close();
           }
        
        }
class Student{
    private String name;
    private String sex;
    private float javascore;
    
    
     public Student(String n, String s, float m)
       {
          name = n;
          sex = s;
          javascore =m;
       }
 
      public String getName(){
    return name;
    }
      public String getSex(){
        return sex;
        }
      public float getJavaScore(){
        return javascore;
        }
}

 

Run the code as follows:

 

Test Procedure 2:

l editing, compiling, debugging and running programs 4-3 (Textbook 116);

l combined result of the program, the program code appreciated that track the use of static fields (netxtId), and static methods (getNextId), and add comments of the associated code;

l comprehension skills Java unit (class) tests.

Results are as follows:

Use static field:

    If the field is defined as static, each class only such a domain. And each object for all instances of a domain but have their own copy. Each employee has its own domain id, but all instances of this class will share a nextId domain. However, only a static field nextid, even without the employee objects, static fields nextId there. It belongs to the class, and not to any separate objects.

Static method:

     Is a static method does not want the operation object implementation method. In both cases you can use the following static methods:

   (1)一个方法不予要访问对象状态,其所需参数都是通过显示参数提供(例如:Math.pow)

   (2)一个方法只需要访问类的静态域(例如:Employee.getNextId)

 

测试程序3:

l 编辑、编译、调试运行程序4-4(教材121);

l 结合程序运行结果,理解程序代码,掌握Java方法参数的用法,在相关代码后添加注释;

/**
 * This program demonstrates parameter passing in Java.
 * @version 1.01 2018-04-10
 * @author Cay Horstmann
 */
public class ParamTest
{
   public static void main(String[] args)
   {
     //该方法不能修改数值参数
      System.out.println("Testing tripleValue:");
      double percent = 10;
      System.out.println("Before: percent=" + percent);
      tripleValue(percent);
      System.out.println("After: percent=" + percent);

    //该方法可以改变对象参数的状态
      System.out.println("\nTesting tripleSalary:");
      var harry = new Employee("Harry", 50000);
      System.out.println("Before: salary=" + harry.getSalary());
      tripleSalary(harry);
      System.out.println("After: salary=" + harry.getSalary());

    //该方法可以将新对象附加到对象参数
      System.out.println("\nTesting swap:");
      var a = new Employee("Alice", 70000);
      var b = new Employee("Bob", 60000);
      System.out.println("Before: a=" + a.getName());
      System.out.println("Before: b=" + b.getName());
      swap(a, b);
      System.out.println("After: a=" + a.getName());
      System.out.println("After: b=" + b.getName());
   }

   public static void tripleValue(double x) // doesn't work
   {
      x = 3 * x;
      System.out.println("End of method: x=" + x);
   }

   public static void tripleSalary(Employee x) // works
   {
      x.raiseSalary(200);
      System.out.println("End of method: salary=" + x.getSalary());
   }

   public static void swap(Employee x, Employee y)
   {
      Employee temp = x;
      x = y;
      y = temp;
      System.out.println("End of method: x=" + x.getName());
      System.out.println("End of method: y=" + y.getName());
   }
}

class Employee // simplified Employee class
{
   private String name;
   private double salary;

   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

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

 

运行结果如下:

测试程序4:

l 编辑、编译、调试运行程序4-5(教材129);

结合程序运行结果,理解程序代码,掌握Java用户自定义类的用法,掌握对象构造方法及对象使用方法,在相关代码后添加注释。

 

对程序的理解:

     在这个程序当中,用了Java的第三种初始化数据域的方法,称为初始化块。在一个类的声明中,可以包含多个代码块。只要构造类的对象,这些块就会被执行。在这个程序的25行开始,无论使用哪个构造器类对象,Id域都在对象初始化块中被初始化。首先运行初始化块,然后才运行构造器的主题部分。

   在这个程序清单中展示了很多特性:重载构造器、用this(...)调用另一个构造器、无参数构造器、对象初始化块、静态初始化块、实例域初始化等。

测试程序5:

l 编辑、编译、调试运行程序4-6、4-7(教材135);

l 结合程序运行结果,理解程序代码,掌握Java包的定义及用法,在相关代码后添加注释;

1)实例4-6

包的放法:

运行结果:

 程序分析:

     在这个程序当中介绍我们介绍了怎么使用类的导入,可以使用import语句导入一个特定的类火整个包,Import语句是一种应用包含在保重的类的简明描述,一旦使用了import语句,在使用类时,就不必写出包的全名了。而且在实验当中也有将类放入包中,就必须将包的名字放在源文件的开头,包中定义类的代码之前。如果没有在源文件中放置package语句,这个源文件中的类就被放置在一个默认包中。默认包是一个没有名字的包。再次之前,我们定义的所有类都在默认包中。

4. 实验总结:

  通过这个周的学习,我明白了用户自定义类是如何去定义的。在第三章我已经学习了如何设计复杂应用程序所需要的各种主力类,同是,这些类没有main方法,却有自己的实例域和实例方法。在学习了类之后,有学习了静态域和静态方法的用法,在这一届的学习过程当中,觉得颇有收获,觉得比较简单易懂,了解了静态域、静态常量、静态方法、工厂方法和main方法的概念,并通过实例程序去真正理解了这些概念。接着学习了对象构造,在这一节主要就学习了参数化,初始化块。最后又学习了包,虽然在之前已经解除了包这个概念,但在这一节有学习了新的概念,比如说类的导入、静态导入、将类放入包中。这些都是自己在自主学习,希望在以后老师的讲解过程中有更大的进步。

 

Guess you like

Origin www.cnblogs.com/JerryLau-213/p/11562974.html