面向对象程序设计(Object Oriented Programming)) oop
面向对象设计思想:主要是避免重复造车轮,将相同属性分类整合
例如:老师类和学生类都有共同的属性:都有名字,都有性别等属性。 所以将姓名和性别属性整合到一个person类中,使用时只需要new一个person就可以。当然不止属性方法也可以。具体例子
属性例子
public class Person {
//persion中有两个属性
String name;
int age;
}
测试类
public class Demo08 {
public static void main(String[] args) {
//Teacher
person teacher = new person();
teacher.name="老师";
teacher.age=40;
//Student
person student = new person();
student.name="学生";
student.age=10;
System.out.println(teacher.name);
System.out.println(teacher.age);
System.out.println(student.name);
System.out.println(student.age);
}
}
方法例子
public class person {
//老师和学生都要吃饭
public void eat(){
System.out.println("吃饭");
}
}
测试类
public class Demo08 {
public static void main(String[] args) {
//Teacher
person teacher = new person();
teacher.eat();
//Student
person student = new person();
student.eat();
}
}