Section 10 Statics, Running Order and Access Level
来源:互联网 发布:wumpus java 游戏编码 编辑:程序博客网 时间:2024/05/22 06:52
Static methods:
static methods/variables do not need instance.
static methods can't use non-static instance variables, nor non-static methods.
You cannot instantiate a class with private constructor.
If you have a class with only static methods, you can marked the constructor private.
Singleton Pattern: You can have a static instance variable of the class and a private constructor.
Static variables:
static variable: value is the same for all instances of the class.
static variables in a class are initialized before any object of that class can be created. When the class is loaded.
static variables in a class are initialized before any static method of the class runs.
static variables are initialized when the class is loaded.
static initializer is a block of code that runs when a class is loaded.
static final int x;static { x=4;}
static final variables are constants. The value of the variable will never change once initialized.
A final variable must be initialized manually.
A final static variable must be assigned a value either at the time it is declared, or in a static initializer.
A final instance variable must be assigned a value either at the time it is declared, or in the constructor.
A final method means you can't override the method.
A final class means you can't extend the class.
Wrapping a primitive.
Boolean, Character, Byte, Short, Integer, Long, Float, Double.
Wrapping a value:
int i = 1;Integer iWrap = new Integer(i);
Unwrapping a value:
int unWrapp = iWrap.intValue();i==iWrap; // trueiWrap.equals(i); // true
Autoboxing:
The compiler does the wrap and unwrap automatically on method arguments, return values, boolean expressions, operations on numbers and assignments.
Format:
int one = 23413423;double two = 42345423.3342;String s = String.format("The rank is %,d out of %,.2f", one, two);
Dates:
String.format("%tc", new Date());Sun Nov 29 14:53:21 MST 2004String.format("%tr", new Date());04:23:13 PMDate today = new Date();String.format("%tA, %<tB %<td", today); (< use the previous argument)
%[argument number][flags(,)][width][.precision] type
For a time-stamp of "now", use Date. But for everything else, use Calendar.
Calendar cal = Calendar.getInstance();
Class Running Order:
public class Order {static {System.out.println("Static Super Class");}Order() {System.out.println("Super Class Constructor");}}class OrderTest extends Order{static{System.out.println("Static Subclass");}OrderTest() {System.out.println("Subclass Constructor");}public static void main(String[] args) {System.out.println("In main");OrderTest ot = new OrderTest();}}
Output:
Static Super Class
Static Subclass
In main
Super Class Constructor
Subclass Constructor
Static is the first thing of loading a class. Static is called before constructor.
Visibility Level:
Interface:
public (abstract) 跨包可见no modifier 包内可见Interface method:
All methods are implicitly public. 接口方法都是public的,都可以跨包访问。不支持super或this。
ModifierInterface继承Interface复写Class复写abstract (no modifier)可以只可覆盖可以default可以可以可以static不可以只可覆盖不可以Default method: Concrete method, can call abstract method.
Static method: Concrete method, can call static method.
Interface variable:
All variables are implicitly public, static, and final. Interface variables are constants. 接口内的变量都是常量。
可继承,可覆盖。
package a;interface InterfaceA {void abstractMethod();static void staticMethod() {System.out.println("InterfaceA static method");}default void defaultMethod() {abstractMethod();System.out.println("InterfaceA default method");}}
InterfaceA只能在包内访问。default 方法内可以调用static方法和abstract方法。
package a;public interface InterfaceAA extends InterfaceA {void abstractMethod();static void staticMethod() {System.out.println("InterfaceAA static method");}// Override /*default void defaultMethod() {System.out.println("InterfaceAA default method");}*/}InterfaceAA可以在包外访问。default方法可以在Interface中继承,也可以在Interface中复写。
测试:
package b;import a.InterfaceAA;class Test implements InterfaceAA {@Overridepublic void abstractMethod() {System.out.println("Test implemented abstract method");}}public class TestDrive {public static void main(String[] args) {InterfaceAA testAA = new Test();testAA.abstractMethod();testAA.defaultMethod(); // Call InterfaceA's method, also can be overridden in test.InterfaceAA.staticMethod();}}在另外一个包中实现InterfaceAA,testAA.defaultMethod()调用的是InterfaceA的default方法。InterfaceA只能在包内访问,但通过继承其default方法可以在包外调用,既方法的访问权限扩大了。Test中也可以复写default方法。
总结:
- 接口的方法和常量的访问权限由接口的访问权限确定,要么包内,要么跨包。
- abstract方法可覆盖(重名,没有body所以算不上复写)。
- default方法可以继承,可以复写。继承可以扩大default方法的访问权限。
- static方法可覆盖,不能被继承,更不能被复写。
- 常量可继承,可覆盖(重名)。
final类不能被继承。
abstract类不能被实例化。类中含有abstract方法必须声明为abstract类。abstract类中可以有concrete方法。
Class Method:
Class Variable:
Modifier包内对象访问包外对象访问类内访问super复写public可以可以跨包跨包只可覆盖protected可以不可以跨包跨包只可覆盖package(no modifier)可以不可以包内包内只可覆盖private不可以不可以类内不支持只可覆盖- Section 10 Statics, Running Order and Access Level
- 《C++ Strategies and Statics》读书笔记
- STATICS
- statics
- 【Leetcode】Binary Tree Level Order Traversal I/II AND Binary Tree Zigzag Level Order Traversal
- Order Book, Level 2 Market Data, and Depth of Market
- LeetCode题解:Binary Tree Level Order Traversal I and II
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)
- LeetCode Binary Tree Level Order Traversal I and II
- LeetCode 之 Binary Tree Level Order Traversal I and II
- Leetcode:BinaryTree level order Traversal and Zigzag traversal
- "Binary Tree Level Order Traversal" and "Balanced Binary Tree"
- 【PAT】【Advanced Level】1119. Pre- and Post-order Traversals (30)
- [Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
- level- order traversal
- C++ Level-Order Traversal
- [leetcode] Level Order Traversal
- Level Order Tree Traversal
- MaterialCheckBox
- Linux多线程编程(三)---线程之间的同步与互斥进阶实验
- Subversion 1.8.13+Apache2.4.6安装
- java的线程同步
- 获取iOS设备信息
- Section 10 Statics, Running Order and Access Level
- 深入 Java 调试体系,第 2 部分: JVMTI 和 Agent 实现
- 深入 Java 调试体系,第 3 部分: JDWP 协议及实现
- 深入 Java 调试体系,第 4 部分: Java 调试接口(JDI)
- 在rials 中使用bootstrap 的modal对话框 实现一个弹窗显示多层次内容的方法
- KE02 实现一次硬件触发转换多个通道
- windows下Qt与lua结合简单介绍
- 使用 Java Debug Interface(JDI)调试多线程应用程序
- 通信系统调制解调基本原理