• 马凯军201771010116《面向对象程序设计(java)》第四周学习总结


    第一部分:理论知识学习部分

      第四章

      1.类与对象的基础概念。

      对象:即数据,对象有三个特性:行为 、状态、标识。

           类是对象,事物的描述和抽象,是具有相同属性和行为的对象集合。对象则是该类事物的实例。

            public:类的访问控制符。Java类具有两种访问访问修饰符:public和default。public允许类具有完全开放的可见性,所有其他类都可以访问它,省略public,则为default,即只有位于同一个包(本质上就是文件夹)中的类可以访问该类。

    abstract指明该类为一个抽象类,说明该类是一个定义不完全的类,需要被继承,才能实例化创建对象。

      类是一个静态的概念,类本身不携带任何数据。当没有为类创建任何对象时,类本身不存在于内存空间中。对象是一个动态的概念。每一个对象都存在着有别于其它对象的属于自己的独特的属性和行为。对象的属性可以随着它自己的行为而发生改变。

      2.对象与对象变量的关系

        a.Java中想使用对象就必须先构造对象,并指定其初始状态。

      4.通过实验掌握了预定义类的基本使用方法,熟悉Math类、String类、math类、Scanner类、LocalDate类的常用API。

      5.掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求

        a.实例域:可将实例域定义为final,构建对象时必须初始化这样的域。

        b.静态域:绝大多数面向对象程序设计语言中,静态域被称为类域。如果将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域却都有自己的一份拷贝。

        c.静态方法:静态方法是一种不能向对象实时操作的方法。可以使用对象调用静态方法。

        d.构造器方法:构造器与类同名。构造器总是伴随着new操作符的执行被调用,而不能对一个已经存在的对象调用构造器来达到重新设置实例域的目的。

        e.更改器方法:调用更改器方法后对象的状态会改变。

        f.访问器方法:只访问对象而不修改对象的方法。

        g.main方法:main方法不对任何对象进行操作。静态的main方法将执行并创建程序所需要的对象。

      6.重载

        多个方法有相同的名字、不同的参数、便产生了重载。Java允许重载任何方法,而不只是构造器方法。

      7.包

             Java允许使用包将类组织起来。借助包可以方便地组织自己的代码,并将自己的代码与别人提供的代码库分开管理。而且使用包可以确保类名的唯一性。

      8.文档注释技术

        有以下几种:类注释,方法注释 ,域注释,通用注释,包与概述注释。

     

    二.实验部分

     

    1、实验目的与要求

     

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

     

    (2) 掌握对象的声明;

     

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

     

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

     

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

     

    2、实验内容和步骤

     

    实验1 测试以下程序,掌握文件输入输出程序设计技术(文件输入输出,教材61-62)

    package aji;
    import java.io.*
    import java.util.*;
    public class FileWriteReadTest {
                   public static void main(String[] args) throws IOException{
                          //写入文件演示
                         PrintWriter out = new PrintWriter("myfile.txt");
                          out.println("姓名 高数 Java 数据结构 平均成绩 总成绩");
                          out.println("张三 20 30 40 0 0");
                          out.println("李四 50 60 70 0 0");
                          out.close();//输出完毕,需要close
                          //读入文件演示
                          Scanner in = new Scanner(new File("myfile.txt"));//为myfile.txt这个File创建一个扫描器in
                          int number = 1;//行号
                          System.out.println(in.nextLine());
                          while(in.hasNextLine()){//判断扫描器是否还有下一行未读取,该循环把文件的每一行都读出
                                 String line = in.nextLine();//读出myfile.txt的下一行
                                 System.out.print(""+(++number)+"行的内容: ");              
                                 Scanner linescanner = new Scanner(line);//行内容建立扫描器
                                 linescanner.useDelimiter(" ");//使用空格作为分隔符
                                 String name = linescanner.next();
                                 String math = linescanner.next();
                                 String java = linescanner.next();
                                 String ds = linescanner.next();
                                 String avg = linescanner.next();
                                 String total = linescanner.next();
                                 System.out.println("name="+name+"  math="+math+"  java="+java+"  ds="+ds+"  avg"+avg+"  total="+total);
                          }
                          in.close();//读入完毕,最后需要对其进行close。
                   }    
    
        }

    结果

    实验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成绩

    (1)源代码:

    (2)Employee.java、 EmployeeTest.java 运行结果截图:

    1 import java.time.*;
     2 
     3 /**
     4  * This program tests the Employee class.
     5  * @version 1.12 2015-05-08
     6  * @author Cay Horstmann
     7  */
     8 public class EmployeeTest
     9 {
    10    public static void main(String[] args)
    11    {
    12       // fill the staff array with three Employee objects
    13       Employee[] staff = new Employee[3];
    14 
    15       staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
    16       staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    17       staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
    18 
    19       // raise everyone's salary by 5%
    20       for (Employee e : staff)
    21          e.raiseSalary(5);
    22 
    23       // print out information about all Employee objects
    24       for (Employee e : staff)
    25          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
    26                + e.getHireDay());
    27    }
    28 }
    29 
    30 class Employee
    31 {
    32    private String name;
    33    private double salary;
    34    private LocalDate hireDay;
    35 
    36    public Employee(String n, double s, int year, int month, int day)
    37    {
    38       name = n;
    39       salary = s;
    40       hireDay = LocalDate.of(year, month, day);
    41    }
    42 
    43    public String getName()
    44    {
    45       return name;
    46    }
    47 
    48    public double getSalary()
    49    {
    50       return salary;
    51    }
    52 
    53    public LocalDate getHireDay()
    54    {
    55       return hireDay;
    56    }
    57 
    58    public void raiseSalary(double byPercent)
    59    {
    60       double raise = salary * byPercent / 100;
    61       salary += raise;
    62    }
    63 }

    3)EmployeeTest.java源代码及运行结果截图如下

    package aji;
    import java.time.LocalDate;
    import java.util.Scanner;
    /**
     * This program tests the Employee class.
     * @version 1.12 2018-09-25
     * @author 自由生长
     */
    public class StudentTest
    {
        String name;
        String sex;
        double score;
       public static void main(String[] args)
        //public static void ScannerTest(){
       {
           
           
           int i = 0;
            
            System.out.print("numer:");
            Scanner sc= new Scanner(System.in);
           int number = sc.nextInt();
           StudentTest Stu[] = new StudentTest[number];
            for (i = 0; i < Stu.length; i++) {
                Stu[i] = new StudentTest();
                System.out.print( (i + 1)+" "+"姓名:" );
                Stu[i].name = sc.next();
                System.out.print( (i + 1)+" " +"性别:");
                Stu[i].sex = sc.next();
                System.out.print( (i + 1)+" " +"java成绩:");
                Stu[i].score = sc.nextDouble();
            }
           
          
          System.out.println("	姓名	性别	成绩");
            for (StudentTest StudentTest : Stu) {
                System.out.println("	" + StudentTest.name+ "	" + StudentTest.sex + "	" + StudentTest.score);
            }
            
            
       }
    }

    测试程序2:

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

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

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

    package aji;
    /**
     * This program demonstrates static methods.
     * @version 1.01 2018-09-23
     * @author 自由生长
     */
    public class StaticTest
    {
       public static void main(String[] args)
       {
          // fill the staff array with three Employee objects
          Employee[] staff = new Employee[3];
    
          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)
          {
             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
    {
       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()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public int getId()
       {
          return id;
       }
    
       public void setId()
       {
          id = nextId; // set id to next available id
          nextId++;
       }
    
       public static int getNextId()
       {
          return nextId; // returns static field
       }
    
       public static void main(String[] args) // unit test
       {
          Employee e = new Employee("Harry", 50000);
          System.out.println(e.getName() + " " + e.getSalary());
       }
    }

    测试程序

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

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

    /**
     * This program demonstrates static methods.
     * @version 1.01 2004-02-19
     * @author Cay Horstmann
     */
    public class StaticTest
    {
       public static void main(String[] args)
       {
          // fill the staff array with three Employee objects
          Employee[] staff = new Employee[3];
    
          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)
          {
             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
    {
       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()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public int getId()
       {
          return id;
       }
    
       public void setId()
       {
          id = nextId; // set id to next available id
          nextId++;
       }
    
       public static int getNextId()
       {
          return nextId; // returns static field
       }
    
       public static void main(String[] args) // unit test
       {
          Employee e = new Employee("Harry", 50000);
          System.out.println(e.getName() + " " + e.getSalary());
       }
    }

    测试程序

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

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

    import java.util.*;
    
    /**
    * This program demonstrates object construction.
    * @version 1.01 2004-02-19
    * @author Cay Horstmann
    */
    public class ConstructorTest
    {
    public static void main(String[] args)
    {
    // fill the staff array with three Employee objects
    Employee[] staff = new Employee[3];
    
    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());
    }
    }
    
    class Employee
    {
    private static int nextId;
    
    private int id;
    private String name = ""; // instance field initialization
    private double salary;
    
    // static initialization block
    static
    {
    Random generator = new Random();
    // set nextId to a random number between 0 and 9999
    nextId = generator.nextInt(10000);
    }
    
    // object initialization block
    {
    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);
    }
    
    // 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()
    {
    return name;
    }
    
    public double getSalary()
    {
    return salary;
    }
    
    public int getId()
    {
    return id;
    }
    }

    测试程序5:

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

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

    import com.horstmann.corejava.*;
    // the Employee class is defined in that package
    
    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)
       {
          // because of the import statement, we don't have to use 
          // com.horstmann.corejava.Employee here
          Employee 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());
       }
    }

     * @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.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;
       }
    }

    实验3  编写长方形类Rectangle与圆形类Circle,其中Rectangle类设置私有属性:width,length;Circle类设置私有属性radius。编写Rectangle类的带参构造函数Rectangle(int width,int length), Circle类的带参构造函数Circle(int radius),编写两个类的toString方法(Eclipse可自动生成)。上述2个类均定义以下方法:

    求周长的方法public int getPerimeter()

    求面积的方法public int getArea()

    在main方法中完成以下任务:

    (1)  输入1行长与宽,创建一个Rectangle对象;

    (2)  输入1行半径,创建一个Circle对象;

    (3)  将两个对象的周长加总输出,将两个对象的面积加总输出。

    package aji;
     import java.util.Scanner;
     public class 实验3 {
     @SuppressWarnings("resource")
     /**
      * This program is the circumference and area of rectangles and circles.
      * time 2018 9 24
     * @author 马凯军
      */
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.println("请输入矩形的x y:"); 
         int x = sc.nextInt();
        int y = sc.nextInt();
         System.out.println("矩形周长 "+Perimeter.getPerimeter(x,y));
         System.out.println("矩形面积"+Area.getArea(x,y));
         
         System.out.println("请输入圆的r:"); 
        int r = sc.nextInt();
         System.out.println("圆周长 "+Perimeter.getPerimeter(r));
         System.out.println("圆面积"+Area.getArea(r));
         
         double c = Perimeter.getPerimeter(x,y)+Perimeter.getPerimeter(r);
         double s = Area.getArea(x,y)+Area.getArea(r);
         System.out.println("矩形与圆的周长和为:"+c);
         System.out.println("矩形与圆的面积和为:"+s);
     
      }
     }
     class Perimeter{
         public static double getPerimeter(double width, double height) {
             return 2*(width + height);
         }
     
         public static double getPerimeter(int r) {
             return 2*3.14*r;
        }
         
     }
    class Area{
        public static double getArea(double width, double height){
             return width * height;
      }
     
         public static double getArea(int r) {
             return 3.14*r*r;
         }
     }

    实验总结    

            通过上一周的学习,我掌握了什么类什么是对象,以及它们之间的关系,也通过程序实践认识到了它们在一个程序中的用途和作用。也让我掌握了预定义类的基本使用方法,如Math类、String类、math类、Scanner类等等;也初步的了解了用户自定义类的语法规则,如实例域、静态域、构造器方法、更改器方法,但是这些知识的掌握不是太好需要进一步的学习,自己要在本周的学习中继续加强。这次学习也掌握了对“重载”、“包”两个词含义的理解。从刚开始学习这门课,慢慢的发现自己从一个个小的零件向大零件进步,前几星期只是学习几句语句,现在学习循环结构,整体布局,块。学习的东西越来越多,以自己现在的水平需要比以前更加用心,花更加多的时间去学习结构、语句。望以后自己在学习上更加努力,刻苦。

  • 相关阅读:
    java泛型
    枚举类与可变参数
    JAVA反射实现JdbcTemplate中查询方法 返回的结果集自动封装成对应的JAVABean对象
    JAVA反射之内省
    JAVA反射基础
    java反射实现将HashMap中的键值对封装为一个JavaBean对象
    hexo配置发布至ssh非22端口服务器
    Exception -LoggerFactory is not a Logback LoggerContext but Logback is on the classpath
    去重优化
    两个域名指向同一服务器的非80端口
  • 原文地址:https://www.cnblogs.com/zero--/p/9706121.html
Copyright © 2020-2023  润新知