java关键字

一、关键字定义:是电脑语言里事先定义的,赋予了指定含义的词语,有特别意义的标识符,有时又叫保留字。
1、根类相关:public private protected class interface extends implement import package abstract
2、根方法函数/属性相关:void return static final
3、循环判断:for switch case do while break continue if 
4、异常:try catch throw throws finally
5:其他:this super
goto(保留字)
二、关键字命名
switch(){//int/shrot/String(JDK1.7版本以前不能用)枚举类型 case ====:break;}
1、访问权限修饰符:
public:共有的、全局的,整个java工程都可以访问
protected:同一个包下的类可以随意访问,跨包不行(跨包继承是可以继承到受保护的属性或者方法)
默认不写:同一个包下的累可以随意访问,跨包不行
private:仅限于当前类可以访问,其他类拥有对象也无法访问私有属性方法
package Test;


public class Student {
public String name = "张三";//任意
protected int age = 10;//类内,包内,子类
String sex = "男";//类内,包内
private int PassWord = 123;//类内
public int getPassWord(){
return PassWord;
}
}


package Test;


public class Class extends Student{
public static void main(String[] args) {
Student stu = new Student();
System.out.println(stu.name);
System.out.println(stu.age);
System.out.println(stu.sex);
System.out.println(stu.PassWord);//间接读取
}
}
package Sun;


import Test.Student;


public class Class extends Student{
public static void main(String[] args) {
Student stu = new Student();
System.out.println(stu.name);
System.out.println(stu.age);
System.out.println(stu.sex);
System.out.println(stu.PassWord);
}
}
如上述代码所示,被private定义属性和方法只能在当前类使用,而在不同包中,继承父类的属性方法,只有被protected定义的属性和方法在子类中才能被使用。

修饰的范围:方法、属性、类
类只能用public和默认不写来修饰
2、this\super
this:当前累的对象
super:父类的对象

普通方法和属性调用:this.方法名(),this.属性名 this()调用构造函数
super.方法名(),super.属性 super();
注意:1、this()和super()必须在第一行

    2、方法重写,如果要对父类的方法进行补充,则需要调用super.父类方法名()方法名定义

3、final(最终的)
可以用来修饰:类、方法、属性、参数、局部变量
修饰类:当前类不能被继承
方法:当前方法不能被重写

属性:代表当前属性只能被赋值一次(常量)

package <pre name="code" class="java">Com;
public class Father {public String name;public Father(final String n){super();}public void eat(){System.out.println("工作");}}
 
package Com;


public class Son extends Father{


<span>	</span>public Son(String n) {
<span>		</span>super(n);
<span>	</span>}
<span>	</span>public void eat(){
<span>		</span>super.work();
<span>		</span>System.out.println("吃东西");
<span>	</span>}
<span>	</span>
}
package Com;

public class ds {
	public static void main(String[] args) {
		Son s = new Son("");
		s.eat();
				
	}
}
如上述所示,打印输出结果为:工作吃东西,super执行父类的方法,final修饰的方法无法被继承当作子类。

4.static(静态的)
可以用来修饰:方法、属性、静态块


注意一点:静态方法中一定不能直接使用非静态属性或者方法

package Com;

public class Apple {
	public static String name;
	//静态块当前类运行时执行
	static{
		System.out.println("执行静态块");
	}
	//代码块创建对象时执行
	{
		System.out.println("执行代码块");
	}
}

package Com;

public class Orange {
	public static void main(String[] args) {
		Apple g = new Apple();
		Apple g1 = new Apple();
		
		g.name = "李武";
		g1.name = "李四";
		Apple.name = "张三";
		System.out.println(g.name );
		System.out.println(g1.name);
		System.out.println(Apple.name);
	}
}
用static修饰的属性,只归属当前类的,对象调用的也是一个属性,所以如上述依次打印输出执行静态块
执行代码块
执行代码块
张三
张三
张三

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值