关键字

来源:互联网 发布:手机淘宝怎样看追评 编辑:程序博客网 时间:2024/05/18 01:06
                                                                                                         关键字
关键字是编程语言里事先定义好并赋予特殊含义的单词,也称作保留字,常用的有以下这么几种:
(1)跟类相关:class、public、private、protected、package、new
(2)跟方法相关:void、return
(3)跟属性相关:final、static
(3)基本数据类型(八大基本数据类型):int、float、double、char、 boolean、long、short、byte
(4)循环结构:while、for、do、break、continue
(5)判断语句:if、else、switch、case
(6)异常:try、catch、finally、final、throws、throw
(7)其他:native、goto、join、instanceof、enum、this、super

1.访问权限
public:当前工程下的任何一个类文件中都可以访问
protected:当前包下的任何一个类和不同包下的子类可以访问默认不写:当前包下的任何一个类可以访问
private:仅仅在当前类里面可以访问
访问权限修饰类:只能使用public和默认不写的访问你权限
注意:一个java文件中可以有多个class类,但是只能有一个public修饰的类
      或者都用默认不写的访问权限
public class Student {public int age;String name;protected char sex;private String adress;private void Study(){adress = "衡阳";}}
2.this、super(1)this:代表当前类的对象,不会固定某一个对象上调用当前类的构造方法:this(参数列表);必须写在第一行格式:    this调用属性:this.属性名    this调用普通方法:this.方法名(参数)    this调用构造方法:this(参数);(2)super:父类的对象格式:    super调用属性:super.属性名    super调用普通方法:super.方法名(参数)    super调用构造方法:super(参数);注意:创建子类的对象的时候,会自动创建父类的对象,用于初始化父类的属性和方法
package guanjianzi2;public class Student {public String name;public void Study(){this.name = "ME";System.out.println(name+"在学习");this.Play();}public void Play(){System.out.println(name+"在玩");}}
package guanjianzi2;public class Text  {public static void main(String[] args) {Student stu = new Student();stu.Study();}}


package guanjianzi2;public class People {public String name;public void Study(){System.out.println("人的Study方法");}}

package guanjianzi2;public class Student extends People {public void Study() {System.out.println("学生的Study方法");super.Study();}}

package guanjianzi2;public class Text  {public static void main(String[] args) {Student stu = new Student();stu.Study();}}







3.final:最终的,表示不能再修改
可以修饰的范围:类、方法、属性、局部变量
final类:代表当前类不能被继承(不能当父类用)
final方法:代表子类不能重写父类的方法
final属性:只能被初始化一次值(常量),而且必须要初始化好
final局部变量:在当前局部变量使用的范围内,不能再被修改

4.static:静态的
可以修饰的范围:方法、属性、静态块

静态属性和方法:在加载类的时候加载到内存当中
静态块
package guanjianzi2;public class Student {static {System.out.println(1);}public static void play() {System.out.println(2);}{System.out.println(3);}public Student() {System.out.println(4);}public void study() {System.out.println(5);}}

package guanjianzi2;public class Test {public static void main(String[] args) {Student stu1 = new Student();Student stu2 = new Student();stu1.play();stu1.study();stu2.play();stu2.study();}}



5、break\continue\return
break:跳出当层循环
continue:结束这一次循环,进入下一次循环
return:结束当前方法
package guanjianzi2;public class Test {public static void main(String[] args) {for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {if(j==1){continue;}System.out.println(j);}}}}


package guanjianzi2;public class Test {public static void main(String[] args) {for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {if(j==1){break;}System.out.println(j);}}}}

package guanjianzi2;public class Test {public static void main(String[] args) {for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {if(j==1){return;}System.out.println(j);}}}}




6、八大基本数据类型
java类中:引用(类)类型、基本数据类型
引用(类)类型:String \JFrame\JLabel\Math
基本数据类型:int float double char long short byte boolean

八大基本数据类型都会对应有一个类类型:
Integer、Float Double Character Long Short Byte Boolean












             
0 0
原创粉丝点击