面向对象的封装package day7; //面向对象的封装 public class Employee { String name = "zhangsan"; private int age; //多个方法重载 public Employee(){ } public Employee(String name,int age){ } public Employee(int age,String name){ } //可以在方法里修改不能访问的属性 public int getAge(){ return age; } public void setAge(int age) { if(age>=1&&age<=120){ this.age = age; }else{ this.age =18; } } public void say(){ System.out.println(name); System.out.println(age); } }
package day7; public class EmployeeTest { public static void main(String[] args) { Employee user = new Employee(); //存在问题的赋值 // user.age = 1000; //使用封装进行属性额隐藏,添加相关的get set方法 //供外部程序调用 user.setAge(50); System.out.println(user.getAge()); } }
结果图;
面对对象继承package day7_1; //继承:只能单继承!!! //子类不能访问父类private成员 //不能继承:子类与父类不在同包,使用默认访问权限的成员, //子类不能访问父类的构造函数 public class Engineer { int no; private String name; private String enterTime; public Engineer() { System.out.println("父类的构造函数"); } public void showInfo() { System.out.println("展示信息"); } }
package day7_1; public class SoftEngineer extends Engineer { private String type; public SoftEngineer() { System.out.println("子类的构造函数"); } public void coding() { super.showInfo(); System.out.println("我在写bug!"); } // 重写:返回值类型, 方法名和参数列表都相同. //访问修饰符不能比父类更严格 @Override public void showInfo(){ super.showInfo(); System.out.println("我是软件工程师"); } }
package day7_1; //在子类里可以通过super关键字调用父类里公开的属性或方法 public class TestEngineer extends Engineer { private String level; public TestEngineer() { System.out.println("子类的构造函数"); } public void test() { System.out.println("我在Debug!"); } public void showInfo() { System.out.println("我是测试工程师"); } //重载 public void showInfo(int i) { System.out.println("我是重载"); } }
package day7_1; public class Test { public static void main(String[] args) { SoftEngineer user1 = new SoftEngineer(); user1.showInfo(); TestEngineer user2 = new TestEngineer(); user2.showInfo(3); } }
结果图:
abstract:限制实例化package day7_4; //abstract:限制实例化,让这个类只能提供继承,只能用于类和方法,不能给属性 public abstract class Instrument { //1:抽象类不能被实例化,只能起到被继承的作用 //2:抽象类里的抽象方法必须被子类实现 除非子类也是抽象类 //3:抽象方法必须放在抽象类里面 //4:抽象类里不一定全是抽象方法 //5:抽象方法没有方法体 String brand; String weight; public abstract void play(); public void show(){ } }
package day7_4; //final 最后的,无法让这个类或方法或属性被继承 public class Piano extends Instrument { final String size = "123"; //重写了父类里面的play方法 @Override public void play(){ System.out.println("我用弹的"); } public final void showCompany(){ System.out.println("made in China"); } }
package day7_4; public class Violin extends Instrument { int length; public void play(){ System.out.println("我是用拉的"); } }
package day7_4; public class Test { public static void main(String[] args) { Piano user1 = new Piano(); user1.play(); Violin user2 = new Violin(); user2.play(); } }
结果图:
Static:静态方法public class User { //成员属性 int age; int height; //类属性 static int weight = 60; //成员方法 public void show(){ // User.age; 成员属性和方法不能直接由类来调用 System.out.print(User.weight); } public static void showWeight(){ System.out.println(User.weight); //在静态方法里不允许调用非静态属性 // System.out.println(User.age); } }
package day7_5; public class UserTest { public static void main(String[] args) { User user = new User(); user.age = 10; User.weight = 100; User.showWeight(); // Arrays.sort(arg0); //1:没有用static修饰的属性和方法,被称为成员属性和成员方法 //成员属性和成员方法只能由对象来调用 //2:使用static修饰的属性和方法被称为类属性, //类属性和类方法可以由类来调用 //也可以由对象来调用 } }
结果图:
时间: 2024-10-14 06:07:47