资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
访问控制:private 私有的protected 受保护的public 公共的类、方法和变量修饰符abstract 声明抽象class 类extends 扩允,继承final 终极,不可改变的implements实现interface 接口native 本地new 新,创建static 静态 strictfp 严格,精准synchronized 线程,同步transient 短暂volatile 易失程序控制语句break 跳出循环continue 继续return 返回do 运行while 循环if 如果else 反之for 循环instanceof 实例switch 开关case 返回开关里的结果default 默认错误处理catch 处理异常finally 有没有异常都执行throw 抛出一个异常对象throws 声明一个异常可能被抛出try 捕获异常包相关import 引入package 包基本类型boolean 布尔型byte 字节型char 字符型double 双精度,float 浮点int 整型long 长整型short 短整型null 空true 真false 假变量引用super 父类,超类this 本类void 无返回值-Java语言有51个保留关键字,其中const和goto虽然被保留但未被使用。你不能使用保留关键字来命名类、方法或变量。一、保留关键字数据类型:Boolean int long short byte float double char class interface流程控制:if else do while for switch case default break continue return try catch finally修饰符: public protected private final void static strictfp abstract transientsynchronized volatile native动作: package import throw throws extends implements this Super instanceof new保留字: true false null goto const二、访问修饰符: 访问修饰符: public , protected , private * 只能应用于类的成员变量。(局部变量只能在函数的范围内可见,不能使用访问修饰符) * 能够用来修饰类本身。(protected , private 不能用于顶级类,只能用于内部类) * 能够应用于成员方法和构造函数。下面是一个例子: package Examples;public class HelloWorld02 /以下定义了三个了类成员变量 public String str1=Hello; /可被你程序中的任何其他代码访问 protected String str2=World!; /程序中同一包的成员和不同包中的该类子类可以访问 private String str3= ; /仅该类中的成员可以访问String str=str1+str3+str2; /不使用修饰符的话,变量的访问控制在所在包内为public, / 不能被所在包外的代码存取/以下定义了三个使用不同修饰符和不同参数的构造方法。 public HelloWorld() System.out.println(str); protected HelloWorld(long l) System.out.print(Use protected constructor! And l is +l+ ); System.out.println(str); private HelloWorld(float f) System.out.print(Use private constructor! And f is +f+ ); System.out.println(str); /声明构造方法为void类型是合法的.不过因为没有返回值,所以不能用来创建新对象. public void HelloWorld() System.out.println(str + Use the void constructor!); public static void main(String args)HelloWorld hw1=new HelloWorld(); /使用无参数的构造方法 HelloWorld hw2=new HelloWorld(5); / 虽然5是int类型,但会自动提升成long类型 HelloWorld hw3=new HelloWorld(5L); HelloWorld hw5=new HelloWorld(3.14f); / 但float类型则必须指明,否则会出错hw5.HelloWorld(); / 无返回值的构造方法只能这样调用 (new HelloWorld().HelloWorld(); /这里创建了一个匿名类对象并调用无返回值的构造方法总结: 请认真思考一下: (1)public、protected、private 可用来修饰哪些成员?使用这些修饰符的成员的访问控制是怎样的?没有指定访问修饰符的成员的访问控制是怎样的?* public、protected和private可以用来修饰类成员变量、方法、构造方法和内部类; public可以用来修饰顶级类,但protected和private则不行。注意事项:* 每一个java文件中可以包含多个类,但只能存在一个public顶级类,如果声明了两个顶级类的话,则会出现编译错误。二、部分其他修饰符 this:Java中定义了this关键字来访问当前对象实例内的成员。当局部变量和类实例内的类变量同名时,在这个局部变量所作用区域内类变量就被隐藏了,必须使用this来指明。 static:有时你希望定义一个类成员,使它的使用完全独立于该类的任何对象。通常情况下,类成员必须通过它的类的对象访问,但是可以创建这样一个成员,它能够被它所在类使用,而不必引用所在类的实例。将类中的成员声明为static就能实现这样的效果。声明为static的变量实质就是全局变量。当声明一个对象(某个类的实例)时,并不产生static变量的拷贝,而是该类所有的实例变量共用同一个static变量。 声明为static的方法有以下三条限制: * 它们只能访问static数据 * 它们仅能调用其他的static方法 * 它们不能以任何方式引用this或super 实例分析: package Examples;public class StaticDemo public static void main(String args) System.out.println(MyStaticClass.str); /不用创建MyStaticClass的实例就能访问它的str变量 / System.out.println(MyStaticClass.str2); 这句是错误的。MyStaticClass msc=new MyStaticClass(); /这里创建MyStaticClass的一个实例 System.out.println(After create instance:); msc.printString(); class MyStaticClass static String str=Hello World!; String str2; void setString(String s) str2=s; static void setNewString(String s)
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号