java核心逻辑第3章 修饰符

上传人:第*** 文档编号:54451194 上传时间:2018-09-13 格式:PPT 页数:43 大小:667.50KB
返回 下载 相关 举报
java核心逻辑第3章    修饰符_第1页
第1页 / 共43页
java核心逻辑第3章    修饰符_第2页
第2页 / 共43页
java核心逻辑第3章    修饰符_第3页
第3页 / 共43页
java核心逻辑第3章    修饰符_第4页
第4页 / 共43页
java核心逻辑第3章    修饰符_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《java核心逻辑第3章 修饰符》由会员分享,可在线阅读,更多相关《java核心逻辑第3章 修饰符(43页珍藏版)》请在金锄头文库上搜索。

1、第3章 三个修饰符,封装的概念 继承的概念 访问修饰符 方法覆盖 super关键字 多态的概念,回顾,static修饰符final修饰符abstract修饰符,本章目标,本章结构,final,三个修饰符,abstract,static,静态属性,静态代码块,静态方法,final修饰变量,final修饰类,final修饰方法,抽象类,抽象方法,static修饰符可以修饰什么? 属性 方法 静态代码块,static修饰符,static修饰属性,static可以修饰属性,不可以修饰局部变量,class TestStatic1 stastic int i; ,public vboid test()st

2、atic int j=10; ,static修饰属性的特点,static修饰的属性全类公有,class MyValueint a;static int b; ,static修饰属性的特点,public class TestStaticpublic static void main(String args)MyValue mv1 = new MyValue();MyValue mv2 = new MyValue();mv1.a = 100;mv1.b = 200;mv2.a = 300;mv2.b = 400;System.out.println(mv1.a);System.out.printl

3、n(mv1.b);System.out.println(mv2.a);System.out.println(mv2.b); ,static修饰属性的特点,static修饰属性的特点,public class TestStatic public static void main(String args)MyValue mv1 = new MyValue();MyValue mv2 = new MyValue();mv1.a = 100;MyValue.b = 200;mv2.a = 300;MyValue.b = 400;System.out.println(mv1.a);System.out.

4、println(MyValue.b);System.out.println(mv2.a);System.out.println(MyValue.b); ,static修饰属性的特点,static修饰属性的特点,static修饰的属性什么时候被初始化? 类加载的时候初始化static修饰的变属性(这是公认的说法,但是是错误的) 还有有三种加载类的方式不会初始化static属性和执行static代码块,见本页注释,第十六章反射部分会详细讲解。,class MyValueint a=10;static int b=20; ,static修饰属性的特点,static修饰的属性什么时候被初始化? 类在连

5、接的准备阶段会给静态基本类型赋值为默认值,比如int赋值为0,double赋值为0.0;为引用类型赋值为null。 在初始化阶段会执行静态变量的显示赋值语句(也就是等号表达式)和静态代码块。,连接,验证,准备,解析,加载,初始化,类的生命周期开始,从加载到使用之前经过的阶段,static修饰的方法称为静态方法静态方法可以通过类名调用 TestStaticMethod.test();,static修饰方法,public class TestStaticMethod public static void test() ,非静态方法无论属性和方法是否是静态的都可以访问,static修饰方法,publ

6、ic class TestStaticMethod2 int a ;static int b;public static void test1()public void test2()public void test3()System.out.println(a);System.out.println(b);test1();test2(); ,静态方法只能访问静态属性和静态方法,static修饰方法,public class TestStaticMethod3 static int a= 10;int b = 20;public void test1()public static void t

7、est2()public static void test3()System.out.println(a);/System.out.println(b); /错误/test1(); /错误test2(); ,静态方法中不能使用this关键字只有实例方法才能覆盖,static修饰方法,class Superpublic static void m()class Sub extends Super public void m() ,静态方法只能隐藏(不是覆盖或重写)静态方法,但是没有多态,static修饰方法,public class TestStaticOverride public stati

8、c void main(String args)Super s = new Sub();s.m();/等效于 Super.m() class Superpublic static void m()System.out.println(“this is super“); class Sub extends Superpublic static void m()System.out.println(“this is sub“); ,静态代码块的概念 在类加载时执行静态代码块的内容(这是公认的说法,但是是错误的,有三种情况会加载类,但是不会执行静态代码块,第十六章反射部分会详细讲解),静态代码块,c

9、lass TestStaticStatementstaticSystem.out.println(“this is static statement“); String name = “suns“;public void test() ,static属性 属于这个类所有 通过“对象名.static属性名”、“类名.static属性名”访问 static方法 通过“类名.static方法名”和“对象名.static方法名”访问 static代码块 主要作用是实现static属性的初始化 当类被载入的时候执行它,且只被执行一次,小结,静态属性,代码块构造方法综合,class StaticCodeB

10、lock static String name = “defname“;static name = “staticname“;System.out.println(“execute static code block“);public StaticCodeBlock() System.out.println(“execute constructor“); ,静态属性,代码块构造方法综合,public class TestStaticCodeBlock static System.out.println(“execute static code block in Test“);public st

11、atic void main(String args) System.out.println(“execute main()“);new StaticCodeBlock();new StaticCodeBlock();new StaticCodeBlock(); ,final修饰符可以修饰什么? 变量 方法 类,final修饰符,final修饰变量(实例变量、静态变量、局部变量) 一旦赋值,其值不可改变(这是公认的说法,但是是错误的,详见第十六章反射) final修饰局部变量 必须先赋值,再使用,如果从未使用,可以不赋值,final修饰变量,public class TestFinalLoca

12、lVar1 public static void main(String args)final int A = 10;/A+ /错误 final int n; ,public class TestFinalLocalVar2 public static void main(String args)final MyClass M1 = new MyClass();M1.value = 20;M1.value = 30;/ M1 = new MyClass();/错误 class MyClassint value; ,final修饰变量,final修饰实例变量时,必须对实例变量显示赋值或提供构造方

13、法对其赋值,final修饰属性,class MyClassfinal int I ; c lass MyClassfinal int I = 10; class MyClassfinal int I ;public MyClass(int i)this.I = i; ,final static一起修饰属性,只能在定义处初始化,final修饰属性,class MyClassfinal int I=20; class MyClassstatic final int I=20; ,final修饰符修饰的方法不能被子类覆盖final修饰符修饰的类不能被继承,final修饰方法和类,abstract修饰

14、符可以修饰什么? 类 方法,abstract修饰符,abstract修饰符修饰的类叫做抽像类,abstract修饰符修饰类,abstract class MyClassint value;public void test()System.out.println(“this is test()“); ,抽象类可以用于声明,但不允许创建对象,抽象类的特点,MyClass m1 ;/正确m1 = new MyClass();/错误,抽象类可以被子类继承,从而创建子类对象,抽象类的特点,abstract class MyClassint value ;public void test()System.

15、out.println(“this is test()“);public abstract void test1(); ,MyClass mc; mc=new MySubClass(); /正确,abstract修饰的方法叫做抽象方法,抽象方法,class MyClassint value ;public void test()System.out.println(“ this is test() “);public abstract void test1(); ,拥有抽象方法的类必须是抽象类,抽象方法的特点,abstract class MyClassint value ;public vo

16、id test()System.out.println(“ this is test() “);public abstract void test1(); ,抽象方法由子类实现,抽象方法的特点,abstract class MyClassint value ;public abstract void test(); class MySubClass extends MyClasspublic void test()System.out.println(“ this is test() “); /* abstract class MySubClass extends MyClass* * */,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号