201871010112-梁丽珍《面向对象程序设计(java)》第四周学习总结

 

项目

内容

这个作业属于哪个课程

<任课教师博客主页链接>https://www.cnblogs.com/nwnu-daizh/

这个作业的要求在哪里

             <作业链接地址>https://www.cnblogs.com/nwnu-daizh/p/11552848.html

作业学习目标

  1. 掌握类与对象的基础概念,理解类与对象的关系;
  2. 掌握对象与对象变量的关系;
  3. 掌握预定义类Date、LocalDate类的常用API;
  4. 掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求;(重点、难点)
  5. 掌握对象的构造方法、定义方法及使用要求;(重点);
  6. 理解重载概念及用法;
  7. 掌握包的概念及用法;

第一部分:总结第四章理论知识

  (1)面对对象程序设计概述

  (2)使用预定义类

    1.类 是具有相同属性和行为的一组对象集合

      类是构造程序的基本单元

      类属于一种抽象数据类型

    2.对象  特性:

        对象的行为——可以对对象施加哪些操作

        对象的状态——当施加那些方法时,对象如何响应?

        对象标识——如何辨别具有相同行为与状态的不同对象?

    3.类和对象

      类是对象的原型    所有属于同一类的对象 都具有相同的特性和操作

      类用于定义实体逻辑   对象是实际的实体

      类是概念模型,定义对象的所有特性和所需的操作   对象是具体的

  (3)知识点概况

 

(4)文档注释

    注释的插入

    类注释

    方法注释

    域注释

    通用注释

    包与概述注释

    注释的抽取

(5)类设计技巧

  1.一定要保证数据私有

  2.一定要对数据初始化

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

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

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

  6.类名和方法名要能够体现他们的职责

  7.优先使用不可变的类

第二部分:实验部分

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

1.  实验目的:

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

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

  (3) 掌握对象的声明;

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

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

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

 

3. 实验步骤与内容:

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

目的:

1)掌握Java语言构造基本程序语法知识(ch1-ch3)

2)利用Java语言基本程序设计知识,学习设计开发含有一个主类、类内可有多个方法的应用程序。

要求:

作业1:公民身份证号码按照GB11643—1999《公民身份证号码》国家标准编制,由18位数字组成:前6位为行政区划分代码,第7位至14位为出生日期码,第15位至17位为顺序码,第18位为校验码。从键盘输入1个身份证号,将身份证的年月日抽取出来,按年-月-日格式输出。注意:输入使用ScannernextLine()方法,以免出错。

输入样例:

34080019810819327X

输出样例:

1981-08-19

代码如下:

package project1;

import java.util.Scanner;

public class ID {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		System.out.println("what is your ID:");
		String ID = s.nextLine();
			int x= Integer.parseInt(ID.substring(6, 10));
			int y= Integer.parseInt(ID.substring(10, 12));
			int z= Integer.parseInt(ID.substring(12, 14));
			System.out.println("Data of birth:" + x + "-" + y + "-" +z);
			s.close();
	}

}

运行结果:

作业2:studentfile.txt文件内容是某班同学的学号与姓名,利用此文件编制一个程序,将studentfile.txt文件的信息读入到内存,并提供两类查询功能:(1)输入姓名查询学号;(2)输入学号查询姓名。要求程序具有友好人机交互界面。

编程建议:

1)从文件中读入学生信息,可以编写如下函数:

public static void StudentsFromFile(String fileName))

2)输入姓名查找学生学号,可以编写如下函数:

public static String findStudent(String name)

3)输入学号查找学生姓名,可以编写如下函数:

public static String findStudent(String ID)

代码如下:

package Package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    private static Student students[];
    public static void main(String[] args) {
        students=new Student[50];
        Scanner in = new Scanner(System.in);
        try {
            readFile("studentfile.txt");
            System.out.println("请选择操作,1按姓名,2按学号,3退出");
            int i;
            while ((i = in.nextInt()) != 3) {
                switch (i) {
                case 1:
                    System.out.println("请输入姓名");
                    String name = in.next();
                    Student student = findStudentByName(name);
                    if (student == null) {
                        System.out.println("没找到");
                    } else {
                        System.out.println(student.toString());
                    }
                    System.out.println("请选择操作,1按姓名,2按学号,3退出");
                    break;
                case 2:
                    System.out.println("请输入学号");
                    String id = in.next();
                    Student student1 = findStudentById(id);
                    if (student1 == null) {
                        System.out.println("没找到");
                    } else {
                        System.out.println(student1.toString());

                    }
                    System.out.println("请选择操作,1按姓名,2按学号,3退出");
                    break;

                default:
                    System.out.println("输入有误");
                    System.out.println("请选择操作,1按姓名,2按学号,3退出");
                    break;
                }

            }
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally {
            in.close();
        }

    }

    public static void readFile(String path) throws IOException {
        FileReader reader = new FileReader(path);
        BufferedReader br = new BufferedReader(reader);
        String result;
        int i=0;
        while ((result = br.readLine()) != null) {
            Student student = new Student();
            student.setName(result.substring(13));
            student.setID(result.substring(0,12));
            students[i]=student;
            i++;
        }
        br.close();
    }

    public static Student findStudentByName(String name) {
        for (Student student : students) {
            if (student.getName().equals(name)) {
                return student;
            }
        }
        return null;

    }

    public static Student findStudentById(String Id) {
        for (Student student : students) {
            if (student.getID().equals(Id)) {
                return student;
            }
        }
        return null;

    }
}

class Student {
    private String name;
    private String ID;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }

    @Override
    public String toString() {
        // TODO 自动生成的方法存根
        return "姓名是:" + name + "学号是:" + ID;
    }
}

 运行如下:

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

测试程序1

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

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

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

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

  姓名    性别 java成绩

 4-2源代码:

import java.time.*;

/**
 * This program tests the Employee class.
 * @version 1.13 2018-04-10
 * @author Cay Horstmann
 */
public class EmployeeTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects (用三个employee对象填充staff数组 )
      Employee[] staff = new Employee[3];					//构造了一个Employee 数组,并填入三个雇员对象

      staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

      // raise everyone's salary by 5%  (把每个人的工资提高5% )
      for (Employee e : staff)    		//利用Employee 类的raiseSalary 方法将每个雇员的薪水提高5%
         e.raiseSalary(5);

      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
            + e.getHireDay());		//调用getName 方法,getId方法和getSalary方法将每个雇员的信息打印出来
   }
}

class Employee				//定义了Employee类
{
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String n, double s, int year, int month, int day)	//输入数据
   {
      name = n;
      salary = s;
      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;
   }
}

4-2代码运行结果:

类的定义与类对象的用法:

类是对象的抽象,而对象是类的具体实例。类是抽象的,不占用内存,而对象是具体的,占用存储空间。

对象是通过new className产生的,用来调用类的方法;类的构造方法 。

  类的语法

  class 类名{

      属性

      行为

      }

类名和文件名要一致,首字母要大写,如果类名是多个单词构成,那么每一个单词首字母都大写。

根据类来创建对象

  语法:

    类名(数据类型) 变量名 = new 类名();

  给对象赋值属性语法:

    对象的变量.属性 = 具体值;

  类的对象的方法的调用:

    对象的变量名字.方法名(参数);

使用”.”运算符访问对象的属性和方法。定义方法:对象.属性:表示调用类之中的属性; 对象.方法():表示调用类之中的方法。

编译Employee.java代码:

package project4;

import java.time.LocalDate;

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

	   public Employee(String n, double s, int year, int month, int day)	//输入数据
	   {
	      name = n;
	      salary = s;
	      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;
	   }
}

运行结果:

编译EmployeeTest.java代码:

package project4;

public class EmployeeTest {
	public static void main(String[] args)
	   {
	      // fill the staff array with three Employee objects (用三个employee对象填充staff数组 )
	      Employee[] staff = new Employee[3];					//构造了一个Employee 数组,并填入三个雇员对象

	      staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
	      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
	      staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

	      // raise everyone's salary by 5%  (把每个人的工资提高5% )
	      for (Employee e : staff)    		//利用Employee 类的raiseSalary 方法将每个雇员的薪水提高5%
	         e.raiseSalary(5);

	      // print out information about all Employee objects
	      for (Employee e : staff)
	         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
	            + e.getHireDay());		//调用getName 方法,getId方法和getSalary方法将每个雇员的信息打印出来
	   }
}

 运行结果:

设计StudentTest.java的源代码:

package project4;

import java.util.Scanner;

public class StudentTest {		//定义StudentTest类
	private String name;
	private String sex;
	private int javascore;
	
	public StudentTest(String n, String s, int j)	//输入数据
	{
		name = n;
		sex = s;
		javascore = j;
	}
	public String getName()		//读取名字
	{
		return name;
	}
	public String getSex()		//读取性别
	{
		return sex;
	}
	public int getJavascore()	//读取成绩
	{
		return javascore;
	}
	public static void main(String[] args)
	{
		System.out.println("请输入学生人数:");
		Scanner in = new Scanner(System.in);
		int number = in.nextInt();
		StudentTest[] score = new StudentTest[number];		//构造了一个StudentTest数组
		for (int i=0;i<number;i++)
		{
			System.out.println("第"+(i+1)+"个学生输入信息"+"姓名:");
				String name = in.next();
			System.out.println("性别:");
				String sex = in.next();
			System.out.println("java成绩:");
				int j = in.nextInt();
				score[i] = new StudentTest(name,sex,j);
		}
		for(int r=0;r<number;r++)
		{
			System.out.println("姓名:"+score[r].getName()+"\t"+"性别:"+score[r].getSex()+"\t"+"java成绩:"+score[r].getJavascore());
		}			//打印信息
		in.close();
	}
}

运行结果:

测试程序2

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

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

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

程序4-3源代码:

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

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

      // print out information about all Employee objects	(打印有关所有员工对象的信息 )
      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(); // calls static method	(通过类名调用静态方法 )
      System.out.println("Next available id=" + n);
   }
}

class Employee					//构造了一个Employee类
{
   private static int nextId = 1;     //添加了一个静态域nextId

   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; // set id to next available id	(将id设置为下一个可用id )
      nextId++;
   }

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

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

实验结果:

静态域(netxtId)与静态方法(getNextId)的用法

  1.静态域
如果将域定义为static,每个类只有一个这样的域。而每一个对象对于所有的实例域都有自己的一份拷贝。

例如,假定需要给每一个记雇员赋予惟一的标识
码。这里给Employee类添加一个实例域id和一个静态域nextId:
class Employee{
 private int id;
 private static int nextId = 1;
}

现在,每一个雇员对象都有一个自己的id域,但这个类的所有实例将共享一个nextId域。换句话说,如果有1000个Employee对象,则有1000个实例域id。但是,只有一个静态nextId。即使没有一个雇员对象,静态域nextId也是存在的。它属于类,而不属于任何独立的对象.

  2.静态方法

静态方法是一种不能向对象实施操作的方法。

例如,Math类的pow方法就是一个静态方法。Math.pow(x,a)计算x的a次幂。在计算时,不使用任何Math对象。因为静态方法不能操作对象,所以不能在静态方法中访问实例域。但是,静态方法可以访问自身类中的静态域。

示例:
public static int getNextId(){
 return nextId;//return static field
}

可以通过类名调用这个方法:  int n = Employee.getNextId();

在下面两种情况使用静态方法:

(1)一个方法不需要访问对象的状态,其所需参数都是通过显示参数来提供(例如:Math.pow())。
(2)一个方法只需要访问类的静态域(例如:Employee.getNextId)。

Java单元(类)测试的技巧

单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。比如我们可以测试一个类,或者一个类中的一个方法。

单元测试好处:它是一种验证行为、设计行为、编写文档的行为、具有回归性。

JUnit跟用main方法测试区别:

JUnit的结果更加直观,直接根据状态条的颜色即可判断测试是否通过,而用main方法你需要去检查他的输出结果,然后跟自己的期望结果进行对比,才能知道是否测试通过。

JUnit让我们同时运行多个测试变得非常方便。

测试程序3

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

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

4-4源代码:

/**
 * 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)
   {
      /*
       * Test 1: Methods can't modify numeric parameters  (测试1:方法无法修改数值参数 )
       */
      System.out.println("Testing tripleValue:");
      double percent = 10;								//按值调用
      System.out.println("Before: percent=" + percent);
      tripleValue(percent);
      System.out.println("After: percent=" + percent);

      /*
       * Test 2: Methods can change the state of object parameters (测试2:方法可以改变对象参数的状态 )
       */
      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());

      /*
       * Test 3: Methods can't attach new objects to object parameters   (测试3:方法不能将新对象附加到对象参数 )
       */
      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;							//将一个参数值增加至3倍
      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  (简化员工类别 )		定义了一个Employee类
{
   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;
   }
}

运行结果:

Java 中方法参数的使用情况:

一个方法不能修改一个基本数据类型的参数 (即数值型或布尔型)。

一个方法可以改变一个对象参数的状态。

一个方法不能让对象参数引用一个新的对象。

测试程序4

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

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

程序4-5源代码:

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)
   {
      // fill the staff array with three Employee objects   (用三个employee对象填充staff数组 )
      var staff = new Employee[3];					////构造了一个Employee 数组,并填入三个雇员对象

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

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

class Employee						//定义了一个Employee类
{
   private static int nextId;       //静态域nextId

   private int id;
   private String name = ""; // instance field initialization(实例字段intialization)
   private double salary;
  
   // static initialization block  (静态intialization块)
   static					//标记关键字static
   {
      var generator = new Random();
      // set nextId to a random number between 0 and 9999   (将nextId设置为0到999之间的随机值)
      nextId = generator.nextInt(10000);		//将雇员的ID的起始值赋予一个小于10000的随机整数
   }

   // object initialization block    (对象intialization块)
   {
      id = nextId;
      nextId++;
   }

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

   public Employee(double s)
   {
      // calls the Employee(String, double) constructor   
      this("Employee #" + nextId, s);   //调用this关键字,调用同一类的另一个构造器。
      									//当调用new Employee(60000)时,Employee(double)构造器将调用Employee(String, double)构造器
   }

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

运行结果:

Java用户自定义类的用法

自定义类:

1. 一种是java中已经定义好的类,如之前用过的Scanner类、Random类,直接拿过来用就可以了。

2.  另一种是需要我们自己去定义的类,我们可以在类中定义多个方法和属性来供我们实际的使用。

定义类的格式

public class 类名{

         定义属性:

    事物的基本特征,可以通过变量来定义属性,比如人的姓名:private String name = “张飞”;

    修饰符 数据类型 变量名 = 值;

   定义方法:

       用来定义该事物的具体功能的。

       修饰符 返回值类型 方法名(参数列表){

    }

  }

对象构造方法及对象使用方法

  构造方法就是类构造对象时调用的方法,主要用来实例化对象。

  使用new + 构造方法 创建一个对象。

  构造方法与类同名且没有返回值。

测试程序5

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

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

程序4-6源代码:

import com.horstmann.corejava.*; 		//import语句导入包

// the Employee class is defined in that package  (Employees类在该包中定义)

import static java.lang.System.*;   //静态导入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)
   {
      // because of the import statement, we don't have to use 
      // 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;
   }
}

运行结果:

Java包的定义及用法:

1.包的本质就属于一个文件夹,用来解决类名称重名的问题。

关键字:package

打包编译命令:  javac -d . 类名.java

-d: 表示生成目录, 根据package定义生成

. :表示在当前所在目录生成子目录

2.包的导入

关键字:import

自动匹配编译顺序(在当前目录下按照主类的使用情况自动编译)

javac -d . ./*.java

3.系统常用包

  java.lang:系统常用基础类(String Object 包装类),JDK1.1之后自动导入

  java.utiljava提供的工具程序包(集合类, ArrayList MashMap),需要手工导入

  jucjava.util.concurrent:并发程序包

4.访问控制权限

private  < default(包访问权限) < protected (继承访问权限)< public

 

4. 实验总结:

    通过本次实验,掌握了类与对象的基础概念,理解类与对象的关系,对象与对象变量的关系,预定义类DateLocalDate类的常用API,户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求,对象的构造方法、定义方法及使用要求,重载概念及用法,包的概念及用法。有些地方理解的还不透彻。对于程序设计的作业,导入studentfile.txt文件的这个编程,感觉还不是很明白,难度较大。这章对象与类的学习是个重点,仍需要多加努力下功夫,才能真正掌握吧,继续努力啊。

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/LZ-728672/p/11563471.html
今日推荐