| 访问级别 | 访问修饰符 | 同类 | 同包 | 子类 | 不同的包 |
| 公开 | public | ![]() |
![]() |
![]() |
![]() |
| 受保护 | protected | ![]() |
![]() |
![]() |
![]() |
| 默认 | 没有访问修饰符 | ![]() |
![]() |
![]() |
![]() |
| 私有的 | private | ![]() |
![]() |
![]() |
![]() |
package wbb.java.modifies;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-17
* Time: 下午2:52
* To change this template use File | Settings | File Templates.
*/
public abstract class AbstractDemo {
public abstract void xxx();
}
abstract class Test extends AbstractDemo {
// public void xxx(){} //用abstract修饰子类,或者重写父类中的抽象方法
}
作用:实现多态。package wbb.java.modifies;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-17
* Time: 下午3:45
* To change this template use File | Settings | File Templates.
*/
public class StaticDemo {
public static final String sqlStr = "SELECT * FROM DUAL";
}
class A extends StaticDemo{
StaticDemo a = new StaticDemo();
public static void main(String[] args) {
System.out.println(StaticDemo.sqlStr);
}
}
一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码是被动执行的.静态代码块的作用可用于初始化类,给类的成员变量赋值等等。
package wbb.java.modifies;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-17
* Time: 下午4:04
* To change this template use File | Settings | File Templates.
*/
public class StaticProgram {
static {
System.out.println("静态代码块执行");
}
public static void main(String[] args) {
//运行结果:输出“静态代码块执行”
}
}
package wbb.java.modifies;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-17
* Time: 下午4:10
* To change this template use File | Settings | File Templates.
*/
public final class FinalDemo {
private static final int num = 1;
public static void main(String[] args) {
// num ++; 被final修饰的变量不能改变
}
}
//class TestFinalClass extends FinalDemo {} 被final修饰的类不能被继承
class TestFinalFun {
public final void finalFun() { }
}
class TestFinalFun2 extends TestFinalFun {
// public void finalFun() { } 被final修饰的方法不能被重写
}
java学习笔记(三)java中的修饰符abstract、static与final,布布扣,bubuko.com
java学习笔记(三)java中的修饰符abstract、static与final
原文:http://blog.csdn.net/w_basketboy24/article/details/37907635