Section 10 Statics, Running Order and Access Level

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; // true
iWrap.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 2004

String.format("%tr", new Date());
04:23:13 PM

Date 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 {
	@Override
	public 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方法。

总结:

  1. 接口的方法和常量的访问权限由接口的访问权限确定,要么包内,要么跨包。
  2. abstract方法可覆盖(重名,没有body所以算不上复写)。
  3. default方法可以继承,可以复写。继承可以扩大default方法的访问权限。
  4. static方法可覆盖,不能被继承,更不能被复写。
  5. 常量可继承,可覆盖(重名)。

Class:

public跨包访问
no modifier包内访问

final类不能被继承。

abstract类不能被实例化。类中含有abstract方法必须声明为abstract类。abstract类中可以有concrete方法。

Class Method:

Modifier包内对象访问包外对象访问类内访问super复写
public可以可以跨包跨包跨包
protected可以复写后可以跨包跨包跨包
package (no modifier)可以不可以包内包内包内
private不可以不可以类内不支持只可覆盖

复写方法时可以increase visibility,但不能reduce visibility。可以将concrete复写成abstract。

Modifier包内类访问包外类访问类内访问父类类名访问复写
public static可以可以跨包跨包只可覆盖
protected static可以不可以跨包跨包可覆盖
package (no modifier) static可以不可以包内包内可覆盖
private static不可以不可以类内不可以可覆盖


Class Variable:

Modifier包内对象访问包外对象访问类内访问super复写
public可以可以跨包跨包只可覆盖
protected可以不可以跨包跨包只可覆盖
package(no modifier)可以不可以包内包内只可覆盖
private不可以不可以类内不支持只可覆盖

Modifier包内对象访问包外对象访问类内访问父类类名访问复写
public static可以可以跨包跨包只可覆盖
protected static可以不可以跨包跨包只可覆盖
package (no modifier) static可以不可以包内包内只可覆盖
private static不可以不可以类内不支持只可覆盖

no modifier限制了在package内访问。protected是继承到包外子类中访问。
在static context中是不能使用generic的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python是一种功能强大的编程语言,广泛用于概率、统计和机器学习领域。 首先,Python拥有丰富的概率计算库,如NumPy和SciPy,可以处理复杂的概率计算和统计分析。这些库提供了很多常用的函数和方法,包括概率密度函数、累积分布函数、随机数生成和统计方法。而且,Python还有很多其他的库,如pandas和matplotlib,可以处理和可视化概率和统计数据。 其次,Python在统计建模方面也非常有用。库如statsmodels和scikit-learn提供了广泛的统计分析工具,包括线性回归、逻辑回归、时间序列分析和贝叶斯模型等。这些库不仅提供了统计方法的实现,还提供了模型评估和结果解释的功能,可以帮助研究者和数据科学家进行复杂的统计分析。 最后,Python在机器学习领域具有广泛的应用。机器学习算法如决策树、支持向量机、神经网络等在Python中有丰富的实现,主要通过scikit-learn和TensorFlow等库提供。这些库不仅提供了机器学习算法的实现,还提供了数据预处理、特征选择、模型评估和交叉验证等功能,帮助用户进行全面的机器学习工作流程。 综上所述,Python在概率、统计和机器学习领域非常有用,其丰富的库和工具使得用户能够进行复杂的概率计算、统计分析和机器学习模型的建立和评估。无论是学术研究还是实践应用,Python都是一种非常适用的编程语言。 ### 回答2: Python是一种广泛应用于概率、统计和机器学习领域的编程语言。它具有丰富的库和模块,可以帮助研究人员和数据科学家在这些领域进行分析和建模。 对于概率和统计来说,Python提供了很多强大的库,如NumPy、SciPy和pandas。NumPy提供了高性能的数值计算能力,尤其适合处理大规模数据集。SciPy则包含了很多常用的统计函数和算法,如概率分布、假设检验和回归分析等。pandas则是一个数据处理和分析的利器,可以轻松处理和转换数据,进行统计计算和绘图。 在机器学习方面,Python也成为了最流行的语言之一。有一些非常有用的机器学习库,如scikit-learn和TensorFlow。scikit-learn提供了大量的机器学习算法和工具,包括分类、回归、聚类和降维等。TensorFlow则是一个开源的深度学习框架,可以用于构建和训练神经网络模型。 Python的易用性和灵活性使得它成为了许多概率、统计和机器学习研究项目的首选语言。它具有简洁而优雅的语法,便于编写和维护代码。同时,Python具有活跃的社区支持,可以获得丰富的学习资源和工具。 总之,Python在概率、统计和机器学习领域具有重要的地位,人们可以利用Python丰富的库和模块来进行高效的数据分析、建模和机器学习任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值