第十周上机练习

1.

package test;
public class Student {
    String name;
    double score;

    public Student() {

    }

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    void set(String name, int score) {
        this.name = name;
        this.score = score;
    }

    void get() {
        System.out.println("姓名:" + name);
        System.out.println("成绩:" + score);

    }

}
package test;
public class LH {
public static void main(String[] args) {
        Student n = new Student();
          n.set("张三", 90);
          n.get();
          Student m = new Student("李四", 80);
          m.get();
    }

}

2.

package test;
public class Person {
    void sayHello() {
        System.out.println("无参的构造方法被调用了");
    }
}
package test;
public class Test {
    public static void main(String[] args) {
        Person a=new Person();
        a.sayHello();
    }
}

3.

package test;
public class Car {
    String name;
    String color;

    public void run() {
        System.out.println(color + "的" + name + "像起飞一样");
    }

    public static void main(String[] args) {
        Car a = new Car();
        a.name = "AE86";
        a.color = "白色";
        a.run();
    }
}

4.

package test;
import java.util.Scanner;
public class Domo {
     public static void main(String[] args) {
         Scanner input=new Scanner(System.in);
         System.out.print("请输入一个数字");
         int a=input.nextInt();
         System.out.print("请再次输入一个数字");
         int b=input.nextInt();
         System.out.println(get(a, b));
     }
     private static int get(int a,int b) {
         return a+b;
     }
}

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

封装,即隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别;将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。

重用、不必关心具体的实现、面对对象三大特征之一、具有安全性。

只带有get器的属性称为只读属性,无法对只读属性进行赋值,只带有set器的属性称为只写属性,无法对其进行引用,同时带有get.set器的属性称为读写属性,且在属性声明中,get,set器必须在属性体的内部声明。

原文地址:https://www.cnblogs.com/z118127/p/12843041.html