201871010114- Li Yansong "object-oriented programming (java)" fourth week of learning summary

 

project

content

This work belongs courses

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

Where this requirement in the job

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

Job learning objectives

  1. Master the basic concepts of classes and objects, understand the relationship between classes and objects;
  2. Grasp the relationship between the object and the object variable;
  3. Master the common API predefined classes Date, LocalDate class;
  4. User-defined master the rules of grammar class, including instance fields, static fields, constructor method, change the filter method, accessor methods, static methods, main method, the method requires the definition of parameters; (major and difficult)
  5. Grasp the object constructor, define methods and requirements; (focus);
  6. Overloaded understand the concept and usage;
  7. Grasp the concept and usage of the package;

Part I: Chapter IV summarizes theoretical knowledge

 

 

4.1 The basic concept of classes and objects

He described the basic concepts of objects and classes, and describes the relationship between the classes.

Program many objects from the standard library, there are some custom.

Structured Programming: by designing a series of process (algorithm), select the appropriate way to solve the problem of storage. Algorithms + Data Structures

4.1.1 Class / Package / Inheritance

Class is a template constructed object, create an instance of the class by the process object class constructor is called.

Package: also called data hiding. From the formal point of view, but the package is a combination of data and behavior in one package, and user object hides the implementation of the data. 4.2.3 advantage

Instance fields: The data object.

Method: Process for manipulating data.

For each object has a particular instance of a particular set of field values. The collection is the current state of the object's value.

The key to directly access the package in other instances of the class domain and must not be allowed methods in the class. Method only program object interacts with the object data. This is the key to improve the reusability and reliability.

Inheritance: In addition to the establishment of a new class by extending a class.

When extending an existing category, the new class after having extended all the attributes and methods of the expanded classes.

In a Java source file can contain only one public class and the file name must match the total class.

4.1.2 object

Behavior of the object: Method object. With all object instances of a class, obtained with the same method of similarity family.

State of the object: How object response when using the method. Describing the current state of the object information characteristic of the object, the state can be changed (otherwise described package was compromised) by calling the method.

Object ID: The state of the object does not fully describe an object. Each object has a unique identity.

4.1.3 The relationship between class

Dependency: use-a, methods for manipulating the object class A, class B, A than relying on B. Interdependence minimum, the minimum degree of coupling.

Polymerization: has-a, A comprises object class of object classes B.

Inheritance: is-a, general to specific,

Drawing using UML class diagrams, describing the relationship between the classes.

4.2 How to use the class

4.2.1 objects and object variables

All Java objects are stored on the heap.

Use constructor initializes the object and constructed.

Object variable does not contain an object, the object variable value elsewhere just an object store references. The return value is a new reference. When an object contains another object variable that still contains only a pointer pointing to another heap objects.

(Corresponding to reference pointer, memory address)

= New new DATE DATE DATE (); 
DATE dead = null; 
if dead = date, the dead refer to the same object date.

4.2.2 does not encourage the use of methods

When the library designers realized that a method should not be there, put it labeled as not to encourage the use, although you can still use the program, but there will be compile-time warnings, but there may be future versions of the library delete.

4.3 Design class

4.3.1 Constructor

Access Level: ways to access the private properties of their class,

Constructor: class with the same name; at least one; do not limit the number of parameters; no return value; always invoked using the new operator.

4.3.2 instance fields

All data fields private, and set accessor and implemented changes to access and modify data fields.

Private data field, change the public with access implements the package, benefits include:

1: internal implementation can be varied, in addition to the methods of this class do not affect other code

2: Change the filter method can perform error checking

If the access is to return a reference to the object variable, we need to clone an object, and then return the object after the clone. Such as the following

Employee em= ... ; Date d= em.getDate(); getDate作为访问器返回了em中一个私有的日期对象,但d也引用了相同的对象,对d操作可以改变em中的值,破坏了封装。

final实例域

可以将实例域定义为final,构建对象时必须对其初始化,且之后的操作中不能在对其修改。

final修饰符大多应用于基本类型或不可变类的域(类中的每个方法都不改变其对象如String类)

4.3.3 方法

对外的方法设置为公有,类内的辅助方法设置为私有。

对于类的设计者而言,公有的方法一定不可以删去,因为其他的代码可能访问到,私有的可以被删去

4.3.4 静态域

将域定义为static,则类中只有一个这样的域,每个对象共享静态域,即使没有对象域也会存在,它属于类不属于对象。使用类名访问。 Math.PI

静态常量:public static final double PI,静态常量不会被修改,所以设置为public

4.3.5 静态方法

通过类使用,不能对对象使用。

因为静态方法不能操作对象,所以不能在静态方法中访问实例域。但是可以访问类中的静态域。

当一个方法不需要访问对象状态,其参数都通过显示参数提供或只需访问类中的静态域则可设置为静态方法。

l  工厂方法

静态方法的一种用途。

如NumberFormat.getCurrnecyInstance(); NumberFormat.getPercentInstance();

无法命名构造器,构造器的名字必须与类名相同,但这里希望得到货币实例和百分比时采用不同的名字。

当使用构造器时,无法改变所构造的对象类型。

4.3.6 方法参数

按值调用:接收调用者提供的值;按引用调用:接收调用者提供的变量地址。

方法可以修改传递引用所对应的变量值,而不能修改传递值调用所对应的变量值。

Java总是按值调用。方法得到的所有参数值是一个拷贝,对象引用及其他的拷贝同时引用一个对象,方法不能修改传递给它的任何参数变量的内容。

"引用"是一个需要内存A存储的"值",方法的参数获得了该"值"的一个副本存储在新的内存B中,方法结束后内存B弃用,仍然没有更改原来内存A中的"值"。

方法内使用的参数与传进去的参数在不同的内存中存储了相同的"引用"的值,该"引用"又指向了某内存块,该内存块实际记录对象里各个实例域的值,对方法内使用的参数的"引用"使用更改器,改变的是内存块中的值,原来的"引用"也是指向这块存储内容变化过的内存块,所以实现了改变对象参数的状态。

public static void swap(Employee x,Employee y)
{
   Employee temp=x; x=y;y=temp;
}
并不会交换传入的两个对象。因为x和y并没有引用原来的对象,而是复制了原来对象的"值(地址)",交换的是x和y
即使是swap(int x,int y)也不会成功。

或者说,按值传递传进去的是变量本身存储的值(对象的值是地址)的副本;按引用传递传进去的是参数本身的地址(基本类型)或者参数存储的地址指向的那一块值(对象),即变量本身。

4.4 对象构造

4.4.1 重载

多个方法具有相同的名字、不同的参数。方法名与参数类型叫做方法的签名。

返回类型不属于方法签名,不能有两个名字相同、参数类型相同却返回不同类型值的方法。

4.4.2 默认域初始化

如果在构造器中没有显式的给域赋初值,会自动为域赋为默认值。

局部变量必须明确初始化,而域如果没有初始化会被自动初始化为默认值。

4.4.3 无参数的构造器

对象由无参数构造器创建时,状态会设置为适当的默认值。

只有类中没有提供任何构造器的时候,系统才会提供一个默认的构造器。也就是说如果提供了一个带参数的构造器而未提供不带参数的,系统不会自动提供不带参数的构造器,在使用不带参数的构造器时会报错。

4.4.4 显示域初始化

确保不管怎样调用构造器,每个实例域都可以被设置为一个有意义的初值是一种很好的设计习惯。

初始值不一定是常量。可以调用方法对域进行初始化。

4.4.5 调用另一个构造器

构造器的第一个语句形如 this(...),这个构造器将调用同一个类的另一个构造器,括号内为参数列表,根据参数选择是哪一个构造器。

public Employee(double s)
{ 
  //calls Employee(String,double)...
  this("Employee ",s);
  ...
}

4.4.6 初始化块

在一个类的声明中,可以包含多个代码块,只要构造类的对象,这些块就会被执行。如:

Class Employee
{
  private static int nextID;   private int id;   private String name;   {id=nextID} //初始化块   public Employee()...   public Employee(String s,int i).. } 无论执行哪个构造器,都会先执行 id=nextID代码块

4.4.7 构造器的处理步骤

基于上述多个途径下的步骤:

1:所有数据域被初始化为默认值。

2:按照在类声明中出现的次序,依次指向所有域初始化语句和初始化块。

3:如果构造器第一行调用了第二个构造器,则执行第二个构造器主体。

4:执行这个构造器的主体。

4.4.8 对象析构

Java有自动的垃圾回收,不支持析构器。

finalize方法将在垃圾回收器清除对象之前调用,在实际应用中不要依赖使用finalize方法回收任何短缺的资源,因为很难知道这个方法什么时候才能够调用。

对于需要在使用完毕后立刻被关闭的资源(如文件),在对象用完时应用close方法来完成清理操作。

4.5 包

Java允许使用包将类组织起来,同时可以确保类名唯一性。

为了保证包名的绝对唯一性,Sun公司建议将公司的因特网域名以逆序的形式作为包名。并对不同的项目使用不同的子包。

4.5.1 类的导入

java.lang包被默认导入。

一个类可以使用所属包中的所有类,以及其他包中的公有类。

使用import 语句导入一个特定的类或者整个包 如 import java.util.*;

import java.util.* 与import java.util.Date; 相比对代码的大小没有任何负面影响。

使用星号只能导入一个包,而非以其前缀的所有包。

大多数情况下只导入所需的包,但在发生命名冲突时需要考虑。如果只用一个则把那一个精准导入,否则在使用类时前面加上完整的包名。

import语句的唯一好处是便捷。

Eclipse中 Source – Organize Imports Pagckage

4.5.2 静态导入

import不仅可以导入类,还可以导入静态方法和静态域。

如 import static java.lang.System.*; 则可直接使用out.println(...);

4.5.3 将类放入包中

在源文件的开头,写上  package  包名 ;

4.5.4 包的作用域

变量显示private,

类、方法、变量若没有设置访问修饰符则包中的所有方法都可以访问。

包密封机制将各种包混杂在一起,不能再向这个包添加类了。

4.6 类路径

类存储在文件系统的子目录中。类的路径必须与包名匹配。

类文件也可以存储在JAR(Java归档)文件中。在一个JAR文件中,可以包含多个压缩形式的类文件和子目录。

为了使类能够被多个程序共享,需要以下几点:

1:把类放到一个目录中,这个目录是包树状结构的基目录。

2:将JAR文件放在一个目录中

3:设置类路径。类路径是所有包含类文件的路径的集合。基目录/当前目录/JAR文件

编译器定位文件,如果引用了一个类而没支出这个类所在的包,编译器首先查找包含这个类的包,并询查所有import执行,确定其中是否包含了被引用的类。如果找到一个以上的类就会产生编译错误,类必须是唯一的,而import语句的次序却无关紧要。

之后还要查看源文件是否比类文件新,如果是那么源文件就会被自动地重新编译。由于只能导入其他包中的公有类而一个源文件只包含一个共有类,所以编译器很容易定位源文件。如果从当前包中导入了一个类,编译器就要搜索当前包中的所有源文件,以便确定哪个源文件定义了这个类。

4.7 文档注释

4.7.1 注释的插入

javadoc,可以由源文件生产一个HTML文档。 javadoc utility从下面几个特性中抽取信息:

公有类和接口

公有的和受保护的构造器及方法

公有的和受保护的域

注释以/** 开始 以 */结束 。自由格式文本。标记由@开始

4.7.2 类注释

类注释必须放在import之后,类定义之前。

4.7.3 方法注释

方法注释必须放在所描述的方法之前,除通用标记之外还可以使用下面的标记

@param 变量描述 可占据多行,可使用html标记,但一个方法的所有@param标记必须放在一起

@return 描述 对当前方法添加 return 部分,可跨多行可使用html标记

@throws类描述 这个标记将添加一个注释,用于表示这个方法可能抛出异常。、

4.7.4 域注释

只需要对公有域(通常只的是静态常量)建立文档

4.7.5 通用注释

以下可用在类文档的注释中

@auther 姓名

@version 版本

@since 始于

@deprecated 类、方法、变量等添加不再使用的注释

@see 引用 用于类、方法中,添加一个超链接。

4.7.6 包与概述注释

可以直接将类、方法和变量注释放在java源文件中,只要以/** ..*/为界就可以了。但想要产生包注释就需要在每个包目录添加一个单独的文件:

1:提供一个以package.html命名的html文件,在body里的所有文本都会被抽取

2:提供一个以package-info.java命名的java文件,这个文件必须包含一个初始的以/** .. */为界定的Javadoc注释,跟随在一个包语句之后。它不应该包含更多的代码或注释。

还可以为所有的源文件添加一个概述性的注释,该注释放置在一个名为overview.html的文件中,并包含在所有源文件的父目录中。body里的所有文本都会被抽取。

4.8 类设计技巧

1:一定要保证数据私有

2:一定要对数据初始化

3:不要在类中使用过多的基本类型

4:不是所有的域都需要设置独立的域访问器和域更改器

5:将职责过多的类进行分解

6:类名和方法名要能够体现它们的职责

第二部分:实验部分

实验名称:实验三 类与对象的定义及使用

1.  实验目的:

(1) 熟悉PTA平台线上测试环境;

(2) 理解用户自定义类的定义;

(3) 掌握对象的声明;

(4) 学会使用构造函数初始化对象;

(5) 使用类属性与方法的使用掌握使用;

(6) 掌握package和import语句的用途。

2.

3. 实验步骤与内容:

实验1 采用个人账号登录https://pintia.cn/,使用绑定码620781加入PTA平台NWNU-2019CST1教学班(西北师范大学 计算机科学与工程学院 2018级计算机科学与技术),完成《2019秋季西北师范大学面向对象程序设计程序设计能力测试1》,测试时间50分钟。

实验2 导入第4章示例程序并测试。

测试程序1:

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

l  结合程序运行结果,掌握类的定义与类对象的用法,并在程序代码中添加类与对象知识应用的注释;

运行注释及运行结果:

 

 

l  尝试在项目中编辑两个类文件(Employee.java、 EmployeeTest.java ),编译并运行程序。

运行结果:

 

 

 

 

 

 

l  参考教材104页EmployeeTest.java,设计StudentTest.java,定义Student类,包含name(姓名)、sex(性别)、javascore(java成绩)三个字段,编写程序,从键盘输入学生人数,输入学生信息,并按以下表头输出学生信息表:

  姓名    性别      java成绩

代码:

package week3;

import java.util.Scanner;

public class StudentTest {
	String name;
	String sex;
	double javascore;
	public static void main(String[] args) {
		System.out.println("Please input the student number:");
		Scanner stu=new Scanner(System.in);
	    int totalStudent=stu.nextInt();
		student[] stus=new student[totalStudent];
		System.out.println("Please input name,sex,score");
		Scanner in=new Scanner(System.in);
		for(int i=0;i<stus.length;i++)
		{
			stus[i]=new student(in.next(),in.next(),in.nextFloat());
		}
		System.out.println("name"+" "+"sex"+" "+" "+"javascore");
		for(student out:stus)
			System.out.println(out.getName()+"  "+out.getSex()+"   "+out.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;
	}
}

  输出结果:

 

测试程序2:

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

l  结合程序运行结果,理解程序代码,掌握静态域(netxtId)与静态方法(getNextId)的用法,在相关代码后添加注释;

l  理解Java单元(类)测试的技巧。

package week4;

/**
 * This program demonstrates static methods.
 * @version 1.02 2008-04-10
 * @author Cay Horstmann
 */
public class StaticTest
{
   public static void main(String[] args)
   {
      // 用三个employee对象填充staff数组 )
      var staff = new Employee[3]; //构造了一个Employee 数组,并填入三个雇员对象

      staff[0] = new Employee("Tom", 40000);
      staff[1] = new Employee("Dick", 60000);
      staff[2] = new Employee("Harry", 65000);

      // 打印有关所有员工对象的信息
      for (Employee e : staff)//调用getName 方法,getId方法和getSalary方法将每个雇员的信息打印出来
      {
         e.setId();
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());
      }

      int n = Employee.getNextId(); //(通过类名调用静态方法 )
      System.out.println("Next available id=" + n);
   }
}

class Employee
{
   private static int nextId = 1;

   private String name;
   private double salary;
   private int id;

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

   public String getName()  //实例域name的访问器方法
   {
      return name;
   }

   public double getSalary() //实例域Salary的访问器方法
   {
      return salary;
   }

   public int getId() //实例域Id的访问方法
   {
      return id;
   }

   public void setId()
   {
      id = nextId; // 将id设置为下一个可用id
      nextId++;
   }

   public static int getNextId()//实例域NextId的访问方法
   {
      return nextId; //返回静态字段
   }

   public static void main(String[] args) // unit test
   {
      var e = new Employee("Harry", 50000);
      System.out.println(e.getName() + " " + e.getSalary());
   }
}

运行结果:

测试程序3:

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

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

package week4;

/**
 * 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);

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

import java.util.*;

/**
 * This program demonstrates object construction.
 * @version 1.02 2018-04-10
 * @author Cay Horstmann
 */
public class ConstructorTest
{
   public static void main(String[] args)
   {
      // 用三个employee对象填充staff数组 
      var staff = new Employee[3];

      staff[0] = new Employee("Harry", 40000);
      staff[1] = new Employee(60000);
      staff[2] = new Employee();

      // 打印有关所有员工对象的信息 
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());
   }
}

class Employee
{
   private static int nextId;

   private int id;
   private String name = "";
   private double salary;
  
   // 静态intialization块
   static
   {
      var generator = new Random();
      //将nextId设置为0到999之间的随机值)
      nextId = generator.nextInt(10000);
   }

   // 对象intialization块
   {
      id = nextId;
      nextId++;
   }

   // 三个重载的构造
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public Employee(double s)
   {
      // calls the Employee(String, double) constructor
      this("Employee #" + nextId, s);
   }

   // the default constructor //错误的构造器
   public Employee()
   {
      // name initialized to ""--see above
      // salary not explicitly set--initialized to 0
      // id initialized in initialization block
   }

   public String getName()//实例域name的访问器方法
   {
      return name;
   }

   public double getSalary() //实例域Salary的访问器方法
   {
      return salary;
   }

   public int getId() //实例域Id的访问器方法
   {
      return id;
   }
}

测试程序5:

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

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

4-6

import static java.lang.System.*;

/**
 * This program demonstrates the use of packages.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class PackageTest
{
   public static void main(String[] args)
   {
      // 因为import语句,我们不必使用
      // com.horstmann.corejava.Employee here
      var harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);

      harry.raiseSalary(5);

      // because of the static import statement, we don't have to use System.out here
      out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
   }
}

4-7代码注释

package com.horstmann.corejava;    //将类放入包中

// the classes in this file are part of this package   (这个文件中的类就是这个包中的一部分)

import java.time.*;   //java.time包的引入

// import statements come after the package statement   (import语句位于package语句之后)

/**
 * @version 1.11 2015-05-08
 * @author Cay Horstmann
 */
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用来引用当前对象
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);     
   }

   public String getName()    //实例域name的访问器方法
   {
      return name;
   }

   public double getSalary()   //实例域Salary的访问器方法
   {
      return salary;
   }

   public LocalDate getHireDay()   //实例域HireDay的访问器方法
   {
      return hireDay;
   }

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

实验运行图

 

 

 

4. 实验总结:

   通过这个第四周的学习,在前面学习中感觉对于q前面知识还是有点欠缺,不能及时的反馈学习,而且从面向过程到面向对象的思维过渡确实理解还是 比较抽象,在学习了类之后,有学习了静态域和静态方法的用法在这一周的觉得颇有收获,觉得比较简单易懂,了解了静态域、静态常量、静态方法、工厂方法和main方法的概念,通过大量的实例更加加深了对于本周知识的理解,能够初步掌握类的构造,以后还是要加深学习的深度,多花点时间在编程练习上。

Guess you like

Origin www.cnblogs.com/liyansong0198/p/11575053.html