package h_m_5_7;
//第一题
public class Student {
private String name;
private int score;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
public static void main(String[] args) {
Student s1= new Student();
s1.setName("王兆明");
s1.setScore(100);
System.out.println(s1);
Student s2 = new Student("嘟嘟", 60);
System.out.println(s2);
}
}
package h_m_5_7;
/*
* 第二题
*/
public class Person {
public Person() {
System.out.println("这个构造方法被调用了");
}
public static void main(String[] args) {
Person s = new Person();
}
}
package h_m_5_7;
/*
* 第三题
*/
public class Car {
String name;
String color;
public Car(String name, String color) {
super();
this.name = name;
this.color = color;
}
public void run(){
System.out.println("正在跑");
}
public static void main(String[] args) {
Car c = new Car("奥迪", "black");
c.run();
}
}
package h_m_5_7;
/*
* 第四题
*/
public class Demo {
public static void sum(int x,int y){
System.out.println("sum:"+(x+y));
}
public static void main(String[] args) {
Demo d= new Demo();
d.sum(20, 30);
}
}
package h_m_5_7;
public class Demo02 {
/*
* 第五题
* 什么是封装 及其get,set 方法的好处
*
* 封装把对象的所有组成部分组合在一起,
* 封装定义程序如何引用对象的数据,封装实际上使用方法将类的数据隐藏起来,
* 控制用户对类的修改和访问数据的程度。
* 适当的封装可以让程式码更容易理解和维护,也加强了程式码的安全性。
*/
}