知识点1:static关键字的使用
* 1. static:静态的
* 2. static可以用来修饰的结构有:属性、方法、代码块、内部类
1. static修饰属性
2. static修饰方法
public class StaticTest {
public static void main ( String [ ] args) {
System . out. println ( Chinese . nation) ;
Chinese c1 = new Chinese ( ) ;
c1. name = "陈" ;
c1. age = 40 ;
c1. nation = "中国" ;
Chinese c2 = new Chinese ( ) ;
c2. name = "姚明" ;
c2. age = 35 ;
c2. nation = "CHN" ;
System . out. println ( c1) ;
System . out. println ( c2) ;
System . out. println ( Chinese . nation) ;
Chinese . show ( ) ;
c1. show ( ) ;
c1. eat ( "米饭" ) ;
methodA ( ) ;
}
public static void methodA ( ) {
System . out. println ( "A" ) ;
}
}
class Chinese {
String name;
int age;
static String nation;
public Chinese ( String name, int age) {
this . name = name;
this . age = age;
}
public Chinese ( ) {
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Chinese{" +
"name='" + name + '\'' +
", age=" + age +
", nation='" + nation + '\'' +
'}' ;
}
public static void show ( ) {
System . out. println ( "我是一个中国人" ) ;
System . out. println ( nation) ;
method ( ) ;
}
public static void method ( ) {
}
public void method1 ( ) { }
public void eat ( String food) {
System . out. println ( name + "喜欢吃:" + food) ;
method1 ( ) ;
System . out. println ( nation) ;
method ( ) ;
}
public static String getNation ( ) {
return nation;
}
public static void setNation ( String nation) {
Chinese . nation = nation;
}
}
3. 应用举例
package com. atguigu. java ;
public class CircleTest {
public static void main ( String [ ] args) {
Circle c1 = new Circle ( ) ;
Circle c2 = new Circle ( 3.3 ) ;
Circle c3 = new Circle ( 3.5 ) ;
System . out. println ( c1) ;
System . out. println ( c2) ;
System . out. println ( c3) ;
System . out. println ( "创建的Circle对象的个数为:" + Circle . getTotal ( ) ) ;
}
}
class Circle {
private double radius;
private int id;
private static int init = 1001 ;
private static int total = init;
public Circle ( ) {
this . id = init++ ;
total++ ;
}
public Circle ( double radius) {
this ( ) ;
this . radius = radius;
}
@Override
public String toString ( ) {
return "Circle{" +
"radius=" + radius +
", id=" + id +
'}' ;
}
public int getId ( ) {
return id;
}
public double getRadius ( ) {
return radius;
}
public void setRadius ( double radius) {
this . radius = radius;
}
public static int getTotal ( ) {
return total;
}
}
package com. atguigu. exer ;
public class AcountTest {
public static void main ( String [ ] args) {
Account account1 = new Account ( ) ;
Account account2 = new Account ( "123456" , 1000 ) ;
System . out. println ( account1) ;
System . out. println ( account2) ;
Account . setAnnualInterestRate ( 0.018 ) ;
Account . setMinBalance ( 1 ) ;
}
}
class Account {
private int id;
private String password = "000000" ;
private double balance;
private static double annualInterestRate;
private static double minBalance;
private static int init = 1001 ;
public Account ( ) {
id = init++ ;
}
public Account ( String password, double balance) {
this ( ) ;
this . password = password;
this . balance = balance;
}
public int getId ( ) {
return id;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
public double getBalance ( ) {
return balance;
}
public void setBalance ( double balance) {
this . balance = balance;
}
public static double getAnnualInterestRate ( ) {
return annualInterestRate;
}
public static void setAnnualInterestRate ( double annualInterestRate) {
Account . annualInterestRate = annualInterestRate;
}
public static double getMinBalance ( ) {
return minBalance;
}
public static void setMinBalance ( double minBalance) {
Account . minBalance = minBalance;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", password='" + password + '\'' +
", balance=" + balance +
'}' ;
}
}
4. 设计模式与单例模式
* 是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式。设计模免去我们自己再思考和摸索。
* 就像是经典的棋谱,不同的棋局,我们用不同的棋谱。
*
* 经典的设计模式有:23 种
创建型模式,共5 种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共7 种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共11 种:策略模式、模板方法模式、观察者模式、迭代器模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
* 二、单例模式( 或单子模式、Singleton )
* 保证在整个的软件系统中,对某个类只能存在一个对象实例
*
* 三、实现方式:① 饿汉式 ② 懒汉式
*
* 对比:懒汉式好:延迟对象的创建,节省内存空间。线程是不安全的。
* 饿汉式好:线程是安全的。
public class SingletonTest {
public static void main ( String [ ] args) {
Bank bank = Bank . getBank ( ) ;
Bank bank1 = Bank . getBank ( ) ;
System . out. println ( bank == bank1) ;
}
}
class Bank {
private Bank ( ) {
}
private static Bank bank = new Bank ( ) ;
public static Bank getBank ( ) {
return bank;
}
}
package com. atguigu. java1 ;
public class SingletonTest1 {
public static void main ( String [ ] args) {
ChairMan c1 = ChairMan . getInstance ( ) ;
ChairMan c2 = ChairMan . getInstance ( ) ;
System . out. println ( c1 == c2) ;
}
}
class ChairMan {
private ChairMan ( ) {
}
private static ChairMan chairMan = null ;
public static ChairMan getInstance ( ) {
if ( chairMan == null ) {
chairMan = new ChairMan ( ) ;
}
return chairMan;
}
}
知识点2:main()的理解
public class MainTest {
public static void main ( String [ ] args) {
Main . main ( new String [ 20 ] ) ;
}
}
class Main {
public static void main ( String [ ] args) {
for ( int i = 0 ; i < args. length; i++ ) {
args[ i] = "args_" + i;
System . out. println ( args[ i] ) ;
}
}
}
package com. atguigu. java1 ;
public class MainDemo {
public static void main ( String [ ] args) {
for ( int i = 0 ; i < args. length; i++ ) {
System . out. println ( "元素" + i + "为:" + args[ i] ) ;
}
}
}
知识点3:类的成员之四:代码块
package com. atguigu. java2 ;
public class BlockTest {
public static void main ( String [ ] args) {
System . out. println ( Person . info) ;
Person p1 = new Person ( ) ;
Person p2 = new Person ( ) ;
}
}
class Person {
String name;
int age;
static String info;
{
System . out. println ( "我是非静态代码块2" ) ;
}
{
System . out. println ( "我是非静态代码块1" ) ;
int num = 10 ;
age = 1 ;
eat ( ) ;
}
static {
System . out. println ( "我是静态代码块2" ) ;
}
static {
System . out. println ( "我是静态代码块1" ) ;
int num = 10 ;
info = "我是人" ;
show ( ) ;
}
public static void show ( ) { }
public void eat ( ) {
System . out. println ( "人吃饭" ) ;
sleep ( ) ;
}
public void sleep ( ) {
System . out. println ( "人睡觉" ) ;
}
public Person ( ) { }
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
}
package com. atguigu. java2 ;
import java. net. SocketTimeoutException ;
public class OrderTest {
public static void main ( String [ ] args) {
Order order = new Order ( ) ;
System . out. println ( order. orderId) ;
Order order1 = new Order ( 3 ) ;
System . out. println ( order1. orderId) ;
}
}
class Order {
{
orderId = 1 ;
}
int orderId = 2 ;
public Order ( ) { }
public Order ( int orderId) {
this . orderId = orderId;
}
}