zoukankan      html  css  js  c++  java
  • 第十周上机

    1、请按照以下要求设计一个学生类Student,并进行测试。
    要求如下:
    1)Student类中包含姓名、成绩两个属性
    2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值.
    3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为姓名和成绩属性赋值
    4)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用方法给姓名和成绩赋值,一个使用有参的构 造方法,在构造方法中给姓名和成绩赋值

    package Pra;
    
    public class Student {
        String name;
        double cj;
    
        Student() {
    
        }
    
        Student(String name, double cj) {
            this.name = name;
            this.cj = cj;
        }
    
        public void set(String name, double cj) {
            this.name = name;
            this.cj = cj;
        }
    
        public void get() {
            System.out.println("该学生姓名为:" + name);
            System.out.println("该学生成绩为:" + cj);
        }
    
    }
    package Pra;
    
    public class TestStu {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Student a = new Student();
            a.set("zhangsan", 80);
            a.get();
            Student b = new Student("lisi", 90);
            b.get();
    
        }
    
    }


    2、请编写一个程序,该程序由两个类组成,一个Person类,一个Test类。在Person类中定义一个无参构造方法,里面 输出一句话:”无参的构造方法被调用了...”。并在测试类中进行测试。

    package haha;
    
    public class Person {
        public Person(){
            System.out.println("无参的构造方法被调用了...");
        }
    
    }
    package haha;
    
    public class TestPer {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Person a = new Person();
        }
    
    }


    3. 使用java类描述一个车类,车都具备名字、颜色两个属性,还具备跑的功能。 请设计一个汽车类Car,该类中包含 两个属性姓名(name)、颜色(color),一个用于描述汽车跑的run()方法。

    package haha;
    
    public class Car {
        String name;
        char color;
    
        public void run() {
            System.out.println(name + "加速快");
        }
    
    }
    package haha;
    
    public class TestCar {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Car a = new Car();
            a.name = "兰博基尼";
            a.color = '黑';
            System.out.println("汽车的名字:" + a.name);
            System.out.println("汽车的颜色:" + a.color);
            a.run();
    
        }
    
    }


    4. 编写一个类,类中定义一个静态方法,用于求两个整数的和。 请按照以下要求设计一个测试类Demo,并进行测试。  要求如下: 
      1)Demo类中有一个静态方法get(int a,int b)该方法用户返回参数a、b两个整数的和; 
      2)在main()方法中调用get方法并输出计算结果。

    package haha;
    
    public class Demo {
        public static void main(String[] args) {
            int a=4;
            int b=6;
            System.out.println("和为:"+get(a,b));
        }
        public static int get(int a,int b){
            return a+b;
            
        }
    
    }


    5.说一下什么是封装, 使用封装的好处。什么是get,set访问器

    隐藏属性、方法或实现细节的过程称为封装
    好处:
    1.隐藏类的实现细节
    2.让使用者只能通过事先定制好的方法来访问数据,可以方便地加
    入控制逻辑,限制对属性的不合理操作
    3.便于修改,增强代码的可维护性
    4.可进行数据检查
    set 是设置属性时进行的操作
    get 是读取属性时进行的操作

  • 相关阅读:
    C 应用
    C 基本语法
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/wxyailjy/p/12841993.html
Copyright © 2011-2022 走看看