Java學習第二十八天<訪問修飾符><封裝><繼承>
阿新 • • 發佈:2022-03-17
訪問修飾符
package chapter07.D1訪問修飾符.modify1; public class A { public int n1=100; protected int n2=200; int n3=300;//預設修飾 private int n4=400; public void m1(){ System.out.println("n1="+n1+"n2="+n2+"n3="+n3+"n4="+n4); //本類四種類型屬性都能訪問 } protected void m2(){} void m3(){} private void m4(){} public void hi(){ m1(); m2(); m3(); m4(); //本類四種類型方法都能訪問 } }
package chapter07.D1訪問修飾符.modify1; public class B { public void say(){ A a = new A(); //在同一個包下可訪問public protected 和預設,不能訪問private System.out.println("n1="+a.n1+"n2="+a.n2+"n3="+a.n3);//訪問不到n4 a.m1(); a.m2(); a.m3();//訪問不到m4 } }
package chapter07.D1訪問修飾符.modify1; public class Test { public static void main(String[] args) { A a = new A(); a.m1(); B b = new B(); b.say(); } }
package chapter07.D1訪問修飾符.modify2; import chapter07.D1訪問修飾符.modify1.A; public class Test { public static void main(String[] args) { A a = new A(); System.out.println(a.n1);//跨包只有public能訪 a.m1();//m2不能 } }
封裝
package chapter07.D2封裝; import java.util.Random; public class AccountTest { public static void main(String[] args) { Account a = new Account("greger", 18, "nfvgoesjfn"); } } class Account{ private String name; private double exmoney; private String key; public String getName() { return name; } public void setName(String name) { if (name.length()>=2&&name.length()<=4){ this.name = name; }else System.out.println("名字長度有誤,預設為zhengtu"); this.name="zhengtu"; } public double getExmoney() { return exmoney; } public void setExmoney(double exmoney) { if (exmoney>20){ this.exmoney = exmoney; }else { System.out.println("餘額不足20,預設為0"); this.exmoney=0; } } public String getKey() { return key; } public void setKey(String key) { if (key.length()==6){ this.key = key; }else { System.out.println("密碼必須6位,默然為123456"); this.key="123456"; } } public void info(){ System.out.println("name="+this.name+"extrmoney="+this.exmoney+"key="+this.key); } public Account(String name, double exmoney, String key) { setName(name); setExmoney(exmoney); setKey(key); info(); } }
package chapter07.D2封裝; import java.util.Random; public class Encapsulation01 { public static void main(String[] args) { /* Person p = new Person(); p.setAge(999); p.setName("efrefowif"); p.setSalary(66); p.setJob("帶電"); p.info();*/ //使用構造器 Person p = new Person(999, 66, "joijoij", "帶電"); } } class Person{ private int age; private double salary; private String name; private String job; //快捷鍵 alt+insert public int getAge() { return age; } public void setAge(int age) { if (age>1&&age<120){ this.age = age; }else { System.out.println("輸入有誤"); Random r = new Random(); this.age= r.nextInt(60); } } public double getSalary() { //可加許可權 return salary; } public void setSalary(double salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { if (name.length()>=2&&name.length()<=6){ this.name = name; }else { System.out.println("名字過短或過長"); this.name= "zhengtu"; } } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public void info(){ System.out.println("neme:"+this.name+"age:"+this.age+"job:"+this.job+"salary:"+this.salary); } public Person(int age, double salary, String name, String job) { setJob(job); setSalary(salary); setAge(age); setName(name); info(); } }
繼承
package chapter07.D3繼承; //父類 public class Base { public int n1=100; protected int n2=200; int n3=300; private int n4=400; public Base(){ System.out.println("Base()父類無參構造器執行...."); } //若是有參構造器覆蓋了無參構造器 public void test100(){ System.out.println("test100"); } protected void test200(){ System.out.println("test200"); } void test300(){ System.out.println("test300"); } private void test400(){ System.out.println("test400"); } //私有屬性要想被訪問,提供一個public方法 public int getN4(){ return n4; } public void calltest400(){ test400(); } }
package chapter07.D3繼承; //子類只能繼承一個父類 public class Sub extends Base{ public Sub() { //隱藏 super(); 預設呼叫父類無參構造器,super和this必須在構造器第一行,兩者不共存 System.out.println("Sub()子類無參構造器執行...."); //若父類是有參構造器,要有super(引數); } public Sub(String dd){ //同理預設呼叫父類無參構造器super(); System.out.println("Sub(String dd)子類有參構造器執行...."); } public void sayOk(){ System.out.println(n1+""+n2+""+n3+""+getN4());//父類非私有屬性和方法可在子類直接訪問,n4私有不行 test100(); test200(); test300();//私有方法不能用 calltest400(); } }
package chapter07.D3繼承; public class ExtendsDetails { public static void main(String[] args) { Sub sub = new Sub(); sub.sayOk(); Sub sub2 = new Sub("jack"); } }
package chapter07.D3繼承; public class ExtendsEexercise01 { public static void main(String[] args) { B b = new B(); } } class A{ A(){ System.out.println("a"); } A(String name){ System.out.println("a name"); } } class B extends A{ B(){ this("abc"); System.out.println("b"); } //預設super(); B(String name){ System.out.println("b name"); } }
package chapter07.D3繼承; public class ExtendsExercise02 { public static void main(String[] args) { C c = new C(); } } class A2{ public A2(){ System.out.println("我是A類"); } } class B2 extends A2{ public B2(){ System.out.println("我是B類的無參構造"); } public B2(String name){ System.out.println(name+"我是B類的有參構造"); }//注意預設 } class C extends B2{ public C(){ this("hello");//本類構造器 System.out.println("我是C類的無參構造"); } public C(String name){ super("hahah"); System.out.println("我是c類的有參構造"); } }
package chapter07.D3繼承; import javax.jws.soap.SOAPMessageHandlers; public class Computer { private String cpu; private int memory; private int disk; public Computer(String cpu, int memory, int disk) { this.cpu = cpu; this.memory = memory; this.disk = disk; } public String getDetails(){ return "cpu="+cpu+"memory="+memory+"disk"+disk;} public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public int getMemory() { return memory; } public void setMemory(int memory) { this.memory = memory; } public int getDisk() { return disk; } public void setDisk(int disk) { this.disk = disk; } }
package chapter07.D3繼承; public class PC extends Computer{//父類沒有無參構造器會報錯 private String brand; //繼承設計基本思想:父類構造器完成父類屬性初始化,子類構造器完成子類初始化 public PC(String cpu, int memory, int disk, String brand) {//自動呼叫構造器 super(cpu, memory, disk);//自動呼叫父類有參 this.brand = brand; } public String getBrand(){ return brand; } public void setBrand(String brand){ this.brand=brand; } public void printInfo(){ System.out.println("PC資訊="); // System.out.println(getCpu()+getMemory()+getDisk()); //呼叫父類getDetails方法,得到相關屬性 System.out.println(getDetails()+"brand="+brand); } }
package chapter07.D3繼承; public class ExtendsExercise03 { public static void main(String[] args) { PC pc = new PC("intel", 16, 500, "IBM"); pc.printInfo(); } }
package chapter07.D3繼承; public class ExtendsTheory { public static void main(String[] args) { Son son = new Son();//記憶體的佈局 System.out.println(son.name);//優先從子類開始訪問,再向上訪問,遇到私有的停止 System.out.println(son.age); System.out.println(son.hobby); } } class GrandPa{ String name="大頭爺爺"; String hobby="旅遊"; } class Father extends GrandPa { String name="大頭爸爸"; int age =39; } class Son extends Father{ String name="大頭兒子"; }