java--继承,访问权限,方法(课堂)



 

publicclass Father {

 public Stringname ="father";//带权限访问修饰符的方法

 String defaultName ="fatherDefaultName";

 publicintage;//

 

 public Father(){

   System.out.println("这个是Father里面的构造方法....");

 }

 

 publicvoid mether(){

   System.out.println("father中的method方法");

 }

}

 

 

package com.lijie.lianxi7;

/*

 *继承的知识点:

 *

 *属性:

 *  1、对象访问属性的顺序,在方法中,如果要访问某个属性时, 

 * 1>找局部变量是否有这个属性(同名)

 *    2>找本类中是否有成员变量(同名)  使用this.属性

 *    3>找父类中的成员变量(同名)         super.属性

 *  

 *    注意点:本类存在某个属性或某个方法, 父类也存在,这个时候,父类的属性或者方法

 *        会被隐藏。

 *  

 * 2、父类属性访问权限

 * 使用public  子类可以访问,不管同包或者不同包

 * default    子类只能在同包下访问

 * protected 子类在同包下访问,如果子类跟父类不在同一个包下,

 *          在子类内部,依然可以访问父类使用protected修饰的属性或方法

 *

 *构造方法:

 * 1、子类继承父类,在创建子类对象时,必须先创建父类的对象

 *    因为:子类访问父类的非静态属性或方法时,需要父类对象(构造方法)进行调用

 *

 * 2、创建子类对象时,必须先创建父类的对象,默认调用父类无参数构造方法

 *    如果想指定调用父类某个一个构造方法,需要使用super([参数列表])

 *   注意:1super([参数类别])必须方法子类构造方法的第一行

 *      2super([参数类别]) this(([参数类别])) 无法同时存在同一个构造方法中

 *      3、父类尽量显示写空参数构造方法,减少不必要的错误

 *

 *普通方法

 

 *

 */

publicclass Sonextends Father {

 

 public Stringname ="son";

 

 public Son(){

   System.out.println("这个是son无参数的构造方法....");

 }

 

 publicvoid method(){

   System.out.println("son中的method方法");

 }

 

 publicvoid info(){

   String name ="局部变量name";

       // this.name -- > son类的对象属性,如果son类没有定义name这个属性,

       //那么this.name表示父类的name属性

       //System.out.println(this.name);

       //super.name ---> father类的对象属性

       //System.out.println(super.name);

      

       //对象访问属性的顺序

       //在方法中,如果要访问某个属性时,

       // 1、找局部变量是否有这个属性(同名)

       // 2、找本类中是否有成员变量(同名)  使用this.属性

       // 3、找父类中的成员变量(同名)     super.属性

  

   System.out.println(name);局部变量name,,由近到远的去查找

     System.out.println(this.name);//son,,由近到远的去查找,但是不找局部变量,只找成员变量

   System.out.println(super.name);//   father,,直接找父类

  

   //this.method();

   //super.method();

  

   System.out.println("是否能调用fatherdefaultName..........");//是否能调用fatherdefaultName..........

   System.out.println(super.defaultName);/// fatherDefaultName

 

  

 }

 

 

 publicstaticvoid main(String[]args) {

  

   Son son =new Son();

   son.info();

  

 

 }

 

}

 

 

 

 

package com.lijie.lianxi7.sub;

 

import com.lijie.lianxi7.Father;

 

publicclass Son2extendsFather{

 

 publicvoid info(){

  

 

   System.out.println("是否能调用fatherdefaultName..........");

   //The field Father.defaultName is not visible

   //System.out.println(super.defaultName);

  

  

   System.out.println("是否能调用protectedName..........");

   System.out.println(super.protectedName);

 }

 

 publicstaticvoid main(String[]args) {

  

   //Son son = new Son();

   //The field Father.protectedName is notvisible

   //System.out.println(son.protectedName);

  

   Son2 son2 =new Son2();

   System.out.println(son2.protectedName);

 }

 

}

 

 

 

 

package com.lijie.lianxi7.sub;

 

import com.lijie.lianxi7.Father;

import com.lijie.lianxi7.Son;

 

publicclass Test {

 

 publicstaticvoid main(String[]args) {

 

 Son son =new Son();//挎包要把相应的包导进来

 //The field Father.protectedName is not visible

 System.out.println(son.protectedName);//跨包,跨类无法访问protectedName修饰的类型,只能访问public修饰的

 

 

 Father f =new Father();//挎包要把相应的包导进来

 System.out.println(f.protectedName);//跨包,跨类无法访问protectedName修饰的类型,只能访问public修饰的

 

 

 

 Son2 son2 =new Son2();

 //The field Father.protectedName is not visible

 System.out.println(son2.protectedName);//跨包,跨类无法访问protectedName修饰的类型,只能访问public修饰的

 

 }

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猿成长

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值