• 王玉兰201771010128《面象对象程序设计》第四周学习总结


    ---恢复内容开始---

     第一部分:理论与学习部分总结:

    (1)类是描述对象的“基本原理”,它定义一类对象所能拥有的数据和能完成的操作,在面象对象的程序设计中,类是程序的基本单元。类之间的关系:依赖,关联,继承。类体是由两部分组成一为实域类,二为定义类方法。

    (2)对象:使用opp清楚对象的三个主要特征:对象的行为,对象的状态,对象标识。

    (3)预定义类的基本使用方法有:Math类、String类、math类、Scanner类、LocalDate类的常用API

    (4)掌握用户定义类中的语法:

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

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

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

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

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

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

    (5)  包可以用packge来申明,可用import来导入包。

    (6)多态性是指“多种形式”

     它使用不同的实例而执行不同操作

     多态包方法是重写或方法重载。

    第二部分:实验部分:

    1、实验目的与要求

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

    (2) 掌握对象的声明;

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

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

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

    2、实验内容和步骤

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

    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。

        }  

    }

     

    运行结果如图:

     存在E盘里的文件如下:

     

     

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

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

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

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

    Employee.java

    package test;
    
    import java.time.LocalDate;
    
    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;
       }
    }
    复制代码
    
    
    

     Employee.Java

     package test;
      
      public class EmployeeTest {
                public static void main(String[] args)
               {
                    // fill the staff array with three Employee objects
                    Employee[] staff = new Employee[3];
      
                  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%
                   for (Employee e : staff)
                      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());
                }
     }
    复制代码

    Employee.java

     EmployeeTest.java 

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

      姓名      性别   java成绩

     import java.util.Scanner;
     2 
     3 public class tudent {
     4     String name;
     5     String sex;
     6     double javascore;
     7     public static void main(String[] args) {
     8         System.out.println("请输入学生人数");
     9         Scanner sc = new Scanner(System.in);
    10         int totalStudent = sc.nextInt();
    11         tudent[] stus = new tudent[totalStudent];
    12         for(int i=0;i<totalStudent;i++){
    13             tudent s = new tudent();
    14             stus[i]=s;
    15             System.out.println("请输入第"+(i+1)+"个学生的姓名");
    16             s.name = sc.next();
    17             System.out.println("请输入第"+(i+1)+"个学生的性别");
    18             s.sex = sc.next();
    19             System.out.println("请输入第"+(i+1)+"个学生的Java成绩");
    20             s.javascore = sc.nextDouble();
    21         }
    22         printtudents(stus);
    23         sc.close();
    24     }
    25  
    26     public static void printtudents(tudent[] s){
    27         System.out.println("姓名	性别	Java成绩");
    28         for(int i=0;i<s.length;i++){
    29             System.out.println(s[i].name+"	"+s[i].sex+"	"+s[i].javascore);
    30         }
    31     }
    32 }
    复制代码

     运行结果如下:

     

    测试程序2:

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

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

    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());
       }
    }
    复制代码

    测试程序3:

    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());
       }
    }
    复制代码

    测试程序4:

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

    l  结合程序运行结果,理解程序代码,掌握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());
       }
    }
    复制代码

    实验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)  将两个对象的周长加总输出,将两个对象的面积加总输出。

    import java.util.*;
     2 
     3 public class shapecount {
     4 
     5     public static void main(String[] args) {
     6         Scanner in = new Scanner(System.in);
     7         System.out.println("输入长:"); 
     8         double length = in.nextDouble();
     9         System.out.println("输入宽:"); 
    10         double width = in.nextDouble();
    11         System.out.println("输入半径:"); 
    12         double radius = in.nextDouble();
    13         Rectangle a=new Rectangle(length,width);
    14         Circle    b=new Circle(radius);
    15         System.out.println("矩形周长:"+a.getPerimeter()+"矩形面积:"+a.getArea());
    16         System.out.println("圆周长"+b.getPerimeter()+"圆面积:"+b.getArea());
    17         double c = a.getPerimeter()+b.getPerimeter();
    18         double d = a.getArea()+b.getArea();
    19         System.out.println("周长和:"+c+"面积和:"+d);
    20     }
    21 
    22 }
    23 
    24 
    25 class Rectangle {
    26     private double width;
    27     private double length;
    28     public Rectangle(double w,double l)
    29     {
    30         width=w;
    31         length=l;
    32     }
    33     public double getPerimeter()
    34     {
    35         double Perimeter = (width+length)*2;
    36         return Perimeter;
    37     }
    38     public double getArea()
    39     {
    40         double Area = width*length;
    41         return Area;
    42     }
    43 }
    44 
    45 class Circle {
    46 
    47     private double radius;
    48     double PI = 3.14;
    49     public Circle(double r)
    50     {
    51         radius=r;
    52     }
    53     public double getPerimeter()
    54     {
    55         double Perimeter = 2*PI*radius;
    56         return Perimeter;
    57     }
    58     public double getArea()
    59     {
    60         double Area = PI*radius*radius;
    61         return Area;
    62     }
    63 }

     第三部分:实验总结:

    第四章主要介绍的是面向对象程序设计,如何创建标准Java类库中的类对象以及如何编写自己的类。需要特别注意的是,在Java中没有类就不能做任何事情,要想使用对象就必须首先构造对象并指定其初始状态,以及在Java中,使用构造(constructor)构造新实例等细节问题。也基本理解了 Java类库中的LocalDate类以及简单的使用定义类的方法等,通过本次实验,凸显出来的问题是在具体程序中,我构造算法与结构还是很差,有了基本的思路但是具体的语言还是是写不出来,在接下来的实验学习中不仅得实实在在,还得经常复习C语言的知识,以及前面学过的,做到温古而知新。

     

     

     

     

     

      

     

     

  • 相关阅读:
    opencv imdecode和imencode用法
    caffe网络中屏蔽某一层的输出Silence层
    Opencv画图操作
    linux读取Windows的txt文件问题
    yolo检测系列
    c提取文件路径、文件名和后缀名
    draknet网络配置参数
    darknet源码解析
    Web_Toy
    Heart thing
  • 原文地址:https://www.cnblogs.com/wang963/p/9706353.html
Copyright © 2020-2023  润新知