自家用近来的java小总结(2.1):类的知识的查漏补缺

首先,这是一篇自己用的文章,不对读者承担任何责任,所以,要带着批判的眼光来看下面的文章

  

 

这篇文章说了些什么?

这文章是我近来8.6号来在编程思想上打的代码,从0~200页的源码接近到在这里,下文正是总结这0~200页的的知识,涉及到接口,内部类.初始化,数值计算的一些细节.此文章不会一下子写完,可能隔一天可能再补下来.因为代码确实有点多..

注意

1 我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确),如果你确信我的内容的话,你可能会损失很大,因为我只是个菜鸟,我只是来补救一些知识的而已,如果不复制(最好自己打上)代码运行一次,可能会被我的在代码的注释所误解, 欢迎提意见和评论~~~你的意见和评论是我更新的动力(促进我更快滴读完编程思想)

本文适合读者

   适合有一定的编程基础的人,因为我的代码可能出错,建议零基础的人找本书先看看,或者一定要复制我的代码运行一下,否则,后果将不堪设想  <( ̄▽ ̄)> 哇哈哈…因为我也只是新手
  

再一次提醒

下面的注释只是我个人的总结.
我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确)
,但是注释的语序凌乱各种天花百草不说了.!!!!绝非书本原话!!!!所以要最好还是把我的代码复制下来,运行一遍,也可以把我的注释删掉,或者提示我说应该怎么说,应该怎么说.

再一次感谢

1 感谢你可以抽出你的时间来进行下面阅读,思考的碰撞是智慧产生的前提.谢谢你的阅读和你愿意复制我的代码并阅读我那千奇百怪无比丑的注释来进行思考的碰撞!~!~

正文:

1.默认构造器的产生时机

public class test{
    test (double i){
        
    }
    test (float i){
        
    }
    public static void main(String[] args) {
//        test a = new test();//报错
                test a = new test(3d);
               test b = new test(3f);
//    因为系统只会在没有定义构造器的时候
//自动生成默认构造器test(){} ,而现在已经定义了test(double i)
//就不会产生默认构造器,所以上面的注释会报错
    }
}

2.成员的初始化

boolean 值会被默认为true

int long short byte会自动默认为0,

而char 为一个空白键,就是空,

而double和float是0.0

引用数据类型,如String和各种类 初始化为null

 1 package thinkingInJava;
 2 public class InitialValues{
 3     public static void main(String[] args) {
 4         InitialValues iv = new InitialValues();
 5         iv.printInitialValues();
 6     }
 7     
 8     boolean t;
 9     char c;
10     byte b;
11     short s;
12     int i;
13     long l;
14     float f;
15     double d;
16     String ss ;
17     InitialValues reference;
18     void printInitialValues(){
19                         //类中的变量会自动被初始化,并且 基本类型大部分都是0,浮点数大部分都是0.0
20                         //char 是 空一个格子,数值0     类大部分都是null;
21         System.out.println("data type  Initial value");
22         System.out.println("boolean    +"+t);
23         System.out.println("char    ["+c+"]");
24         System.out.println("byte    "+b+"");
25         System.out.println("short    "+s+"");
26         System.out.println("int    "+i+"");
27         System.out.println("long    "+l+"");
28         System.out.println("float    "+f+"");
29         System.out.println("Double    "+d+"");
30         System.out.println("String    "+ss+"");
31         System.out.println("Reference    "+reference+"");
32     }
33 }

3.可以用方法来对成员进行赋值

 1 public class testMehod{
 2         int i=f();
 3         int j  = f(i);
 4     int f() {int j = 3;return 11;};
 5                         //原来还可以这样做...  之前还认为在 方法外不能调用方法     这句话可能是正确的,但是赋值就打破了这一点.
 6                         //原来还可以直接运行方法体的..?
 7                         //从上到下读,并且还会追朔方法的吧?
 8     int f(int i){  return 22;}
 9     public static void main(String[] args) {
10         
11     }
12 }

4.初始化的总结

1.初始化的时候,要看看
    1.该类有没有父类
1.1       1.1有    到父类
1.1   找static,static域初始化(static和static域的优先级是等同的,从上到下谁写在前就运行谁 
1.1.*        (在这里也要注意的是,如果在static里面碰到了 new了一个对象)
1.1             会先不看static域的所有东西,而去在那对象里开始找定义和构造函数进行初始化),
阿里的面试题说明了这一点http://blog.csdn.net/ysjian_pingcx/article/details/19605335
1.2          到    子类
1.2   找static,static域初始化(也是从上到下读static和static域()}
1.3           再返回父类,在找定义语句和域语句{}进行初始化(也是优先级等同的,所以是从上到下)
1.4               再到父类的构造函数

1.5 再在到子类,在找定义语句和域语句{}进行初始化
1.6  再到子类的构造函数
                

  

 4. 1.定义初始化

 1 package thinkingInJava;
 2  
 3 
 4 public class testMehod{
 5         public static void main(String[] args) {
 6             House h = new House();
 7         }
 8 }
 9 class Window{
10     Window(int maker){
11         System.out.println("Window ("+maker+") ");
12     }
13 }
14 class House{
15     Window w1 = new Window(1);
16     House(){
17         w3 = new Window(33);
18     }
19     Window w2 = new Window(2);
20     void f(){
21         System.out.println("f()");
22     }
23     Window w3 = new Window(3);
24 }

4.2.静态初始化

package thinkingInJava;

            //要注意的是,main方法进行前还是先是进行类里面的static初始化,但是没有涉及
                //到的时候,是不会进行静态初始化的,不要认为static 在main外的类里面,就必须要先将他们读完
                //如果没用用到那个类的话,是不会初始化static的
public class testMehod{
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
    
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        
        table.f2(1);
        cupboard.f3(1);
        
    }
}
class Table{
    static Bowl bowl1 = new Bowl(1);
    
    
    Table(){
        System.out.println("Table() ");
        bowl2.f1(1);
    }
    
    
    void f2(int marker){
        System.out.println("f2(" +marker +")");
    }
    
    
    static Bowl bowl2 = new Bowl(2);
    
}
class Cupboard{
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);
    
    
    Cupboard(){
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }
    
    
    void f3(int marker){
        System.out.println("f3 ("+marker +") ");
    }
    
    
    static Bowl bowl5 = new Bowl(2);
}


class Bowl{
    Bowl (int marker){
        System.out.println("bowl ("+marker+")");
    }
    
    
    void f1(int marker){
        System.out.println("f1(    " +marker+")");
    }
    
}

4.3.普通员域初始化

package thinkingInJava;

public class Mugs{
        public static void main(String[] args) {
            System.out.println("Inside main()");
            new Mugs();
            
            System.out.println("new Musgs() completed    ");
            
            new Mugs(1);
            System.out.println("new Musgs(1) completed    ");
        }
        
        //fen jie xian------------------------
        Mugs(int i){
            System.out.println("Mug(int )");
        }
        Mugs(){
            System.out.println("Mugs()");
            
        }
        
        Mug mug1 ;
        Mug mug2;
        {
            mug1 = new Mug(1);
            mug2 = new Mug(2);
        }
}
class Mug{
    Mug(int marker){
        System.out.println("Mug ("+marker +")");
    }
    void f(int marker){
        System.out.println("f("+marker+")");
    }
}

5.enum类的知识

5.1.注意,test里面的类都是static final class

   这里让我联系到了static内部类,内部类的知识就呈上来了

package thinkingInJava;


enum test{
	A,B,C,D;
}
public class OverLoading{
	public static void main(String[] args) {
		test a = test.A;
		System.out.println(a);  //enum 类重载了toString方法,直接打出
	}
}

5.1.1静态内部类

         class A{ static class B{}}静态内部类用(外部类).(A)来直接创建对象 而   原来的非静态(class A { class B{}}内部类是依赖一个外部类来进行对象的创建,

   以下是静态内部类的例子.

package test.java;
public class Parcel11{
	public static class ParcelContents implements Contents
	{
		private int i=1;;
		public int value()			//普通的内部类是不能声明static方法的.我知道啊
		{
			return i;
		}
	}
	
	protected static class ParcelDestination implements Destination
	{
		private String label;
		private ParcelDestination(String whereTo)
		{
			label = whereTo;
		}
		
		public String readLabel()
		{
			return label;
		}
		
		public static void f(){}
		static int x = 10;
		
		static class AnotherLevel
		{
			public static void f(){}
			static int x =10;
		}
	}
	
	public static Destination destination (String s )
	{
		return new ParcelDestination(s);
	}
	
	public static Contents contents()
	{
		return new ParcelContents();
	}
	
	public static void main(String[] args) {
		Contents c = contents();			//  类里面嵌套static类 ,然后全部都变成static方法是搞那样
		Destination d = destination("ABC");  //这样子就完全不依赖内部类对象了, static类的话,只要一个上溯造型,就可以引出家了
				
	}

}

interface  Contents
{
	}
interface Destination
{
	}

  

 

 5.2  foreach 语法 来输出 enum ,  注意 test.values()

package thinkingInJava;


enum test{
	A,B,C,D;
}
public class OverLoading{
	public static void main(String[] args) {
		
		for (test temp:test.values()){   //要注意test.values() 就好像 一个数组名一样  for (  变量: 数组)
		System.out.print(temp+" ");  //enum 类重载了toString方法,直接打出
		System.out.println(temp.ordinal());  // 输出他的序号,用来case不错.
		}
	}
}

  

5.3用switch来最实用滴调用enum

package thinkingInJava;
enum testA{
	A,B,C,D;
}

public class Burrito{
			//这个另我领悟了包的重要性...因为不小心重叠了一些类的名字,莫名的报错,打包了之后就没有这些错的发生了
	public static void main(String[] args) {
		Burrito plain = new Burrito(testA.A),
		greenChile = new Burrito(testA.B),
			jalapeno = new Burrito(testA.C);
		
		plain.decribe();
		greenChile.decribe();
		jalapeno.decribe();
	}
	
	// 方法区和变量区
	testA degree;
	public Burrito(testA degree){
		this.degree = degree;   //可以设置一个 enum变量
												//然后用 switch 来得到相应的描述
	}
	public void decribe(){
		System.out.println("This burrito is ");
		switch(degree){
		case A:
			System.out.println("A");
			break;
		case B:
			System.out.println("B");
			break;
		case C:
			System.out.println("C");
			break;
		case D:
			System.out.println("D");
			break;
		default:
			System.out.println("maybe hot ");
			break;
		}
	}
}

  6.引用static类来创建自己的工具库

  

package net.mindview.util;
  //下次用的时候直接诶用 import static net.mindview.util.Print.*;
//你会稳为什么要import static ,因为他是导入静态方法
  //你也会稳为什么导入 improt static net.mindview.util.*;为什么会报错
  //因为import static只能导入全部都是静态方法的语句
import java.io.PrintStream; public class Print{ public static void print(Object obj){ System.out.println(obj); } //打印一个信行 这样之后就不用换行了 public static void print(){ System.out.println(); } public static void printnb(Object obj){ System.out.print(obj); } public static PrintStream printf(String format,Object...args){ return System.out.printf(format,args); //这个用来干吗的, new javaSE5 printf FROM C; } } ********************************* 测试 package thinkingInJava; import static net.mindview.util.Print.*; //全部静态方法的导入 public class PrintTest{ public static void main(String[] args) { print("新的方法啊.,爽爆了"); print(100); print(100L); print(3.14159); } } package net.mindview.util; public class Range{ public static int[] range(int n){ int[] result = new int[n]; for ( int i =0 ; i<n ; i++){ result[i] = i; } return result; //创建工具去返回一个 result数组, } public static int[] range(int start,int end){ int sz = end-start; int[] result = new int[sz]; for ( int i=0 ; i <sz ; i++){ result[i] = start+i; } return result; //返回一个范围数组 } public static int[] range(int start,int end,int step){ int sz = (end-start)/step; int[] result = new int[sz]; for ( int i=0; i <sz ; i++){ result[i] = start+(i*step); } //返回一个1,3,5,7的数组 return result; } }

  

另外补充..finalize的备用

System.gc()(在运行程序的时候,该函数不一定会运行)会自动调用finalize() 方法,该方法是用户手动清理对象的函数

package thinkingInJava;

public class terminationCondition{
    public static void main(String[] args) {
        Book novel = new Book(true);
        //Proper cleanup
        novel.checkIn();
        
        //Drop the reference,forget to lean up:
        new Book(true);
        
        //Force garabage collection & finalization:
        System.gc();   
    }
    
}

class Book{
    boolean checkOut = false;
    Book(boolean checkOut){
        checkOut = checkOut;
    }
    void checkIn(){
        checkOut = false;
    }
    
    protected void finalize(){
        System.out.println("abc");
        if ( checkOut){
            System.out.println("Error : check out");
            //也可用 super finalize()
        }
    }
}
原文地址:https://www.cnblogs.com/davidway/p/3905692.html