JAVA基础1(代码剖析)

代码选自善学堂JAVA培训
</pre><pre class="html" name="code">public class TestBinaryNum {
	
	public static void main(String[] args){
		int a = 0b0000_0000_0000_0000_0000_0000_0000_0011;
		int b = 1_2312_3131;
		System.out.println(a);
		System.out.println(b);
	}
}


/**
 * 测试JDK7中的switch新特性
 * @author dell
 *
 */
public class TestSwitch02 {
	public static void main(String[] args) {
		String  a = "高琪";
		
		switch (a) {   //JDK7的新特性,表达式结果可以是字符串!!!
		case "马士兵":
			System.out.println("输入的马士兵");
			break;
		case "高琪":
			System.out.println("输入的高琪");
			break;
		default:
			System.out.println("大家好!");
			break;
		}
		
	}
}


/**
 * 测试内部类的使用
 * @author dell
 *
 */
public class Outer {
	public static void main(String[] args) {
		Face f = new Face();
		Face.Nose n = f.new Nose();
		n.breath();
		Face.Ear e = new Face.Ear();
		e.listen();
	}
}

class Face {
	int type;
	String shape="瓜子脸";
	static String color="红润";
	
	class Nose {
		
		void breath(){
			System.out.println(shape); 
			System.out.println(Face.this.type);
			System.out.println("呼吸!");
		}
	}
	
	static class Ear {
		void listen(){
			System.out.println(color); 
			System.out.println("我在听!"); 
		}
	}
	
}


继承1:

public class Animal {
	String str;
	public void voice(){
		System.out.println("普通动物叫声!");
	}
}

class Cat extends Animal {
	public void voice(){
		System.out.println("喵喵喵");
	}
	public void catchMouse(){
		System.out.println("抓老鼠");
	}
}

public class Test {
	
	public static void testAnimalVoice(Animal c){
		c.voice();
		if(c instanceof Cat){                   //instance
			((Cat) c).catchMouse();
		}
	}
	

	public static void main(String[] args) {
		Animal a = new Cat();
		Cat a2 = (Cat)a;
		testAnimalVoice(a);
		
	}
}


多态:

public class HttpServlet {
	public void service(){
		System.out.println("HttpServlet.service()");
		this.doGet();
	}
	
	public void doGet(){
		System.out.println("HttpServlet.doGet()");
	}
	
}

public class MyServlet extends HttpServlet {
	
	public void doGet(){
		System.out.println("MyServlet.doGet()");
	}
	
}

public class Test {
	public static void main(String[] args) {
		HttpServlet s = new MyServlet();
		s.service();  //这里service里边调用的是 myservlet里边的doGet方法
	}
}

输出:HttpServlet.service()
           MyServlet.doGet()



静态代码初始化块:在类一开加载的时候就执行 只执行一次

public class TestStaticInitBlock extends Parent001 {
	
	static int a ;
	
	static {
		System.out.println("静态初始化TestStaticInitBlock!");
		a = 100;
	}
	
	
	public static void main(String[] args) {
		
	}
}

public class Parent001 /*extends Object*/ {
	static int aa;
	static {
		System.out.println(" 静态初始化Parent001");
		aa=200;
	}
}
输出: 静态初始化Parent001
           静态初始化TestStaticInitBlock!


final

public /*final*/ class Animal {    //final修饰类则说明,这个类不能被继承!
	
	public /*final*/ void run(){   //final加到方法前面,意味着该方法不能被子类重写!
		System.out.println("跑跑!");
	}

}

class Bird  extends Animal {
	
	public void run(){
		super.run();
		System.out.println("我是一个小小小小鸟,飞呀飞不高");
	}
	
}

public class TestFinal {
	public static void main(String[] args) {
		final int MAX_VALUE= 200;    //常量。
		double d = Math.PI;
	}
}


this关键字:

public class Student {
	 String name;
	 int id;   
	 
	 public  Student(String name,int id){
		 this(name);   //相当于调用下边的构造方法
              //通过this调用其他构造方法,必须位于第一句! Constructor call must be the first statement in a constructor
		 this.id = id;
	 }
	 
	 public Student(String name){
		 this.name = name;
	 }
 
	 public void setName(String name){
		 this.name = name;
	 }
	 
	 public void study(){
		 this.name=  "张三";
		 System.out.println(name+"在學習");
	 } 
	
}


重载

public class TestOverload {
	public int add(int a, int b){
		return a+b;
	}
	public static void main(String[] args) {
		MyMath m = new MyMath();
		int result = m.add(4.2,8);
		System.out.println(result);
	}
}


class  MyMath {
	int a;
	int b;
	
	public MyMath(){
	}

	public MyMath(int a){
		this.a = a;
	}
	
	public MyMath(int b, int a){
		this.b = b;
		this.a = a;
	}
	
	public int add(int b, double a){
		return (int)(a+b);
	}
	
	public int add(double a, int b){
		return (int)(a+b);
	}
	
	public int add(int a, int b){
		return a+b;
	}
	
	public int add(int a, int b,int c){
		return a+b+c;
	}
	
}

数组:

class Car {
	String name;
	public Car(String name){
		this.name=  name;
	}
}

/**
		 * 1. 数组是相同数据类型(数据类型可以为任意类型)的有序集合
		 * 2. 数组也是对象。数组元素相当于对象的成员变量(详情请见内存图)
		 * 3. 数组长度的确定的,不可变的。如果越界,则报:ArrayIndexOutofBoundsException
		 */
public class Test02 {
	public static void main(String[] args) {
		//声明
		int[] a;
		int b[];
		//创建数组对象
		a = new int[4];
		b = new int[5];
		
		Car[] cars1 = new Car[4];
		cars1[0] = new Car("奔驰");
		
		Car c2 = new Car("奔驰");
		
		
		
		//初始化(对数组元素的初始化)
		//默认初始化:数组元素相当于对象的成员变量,默认值跟成员变量的规则一样。数字0,布尔false,char\u0000,引用:null
		//动态初始化:
		for(int i=0;i<a.length;i++){
			a[i] = i*12;
		}
		
		//静态初始化
		int c[] = {23,43,56,78};   //长度:4,索引范围:[0,3]
		Car[] cars = {
						new Car("奔驰"),
						new Car("比亚迪"),
						new Car("宝马")
					};
		System.out.println(c2==cars[0]); //false	
	}
}

/**
 * 模拟实现JDK中提供的ArrayList类
 * @author dell
 *
 */
public class MyArrayList {
	/**
     * The value is used for object storage.
     */
    private Object[] value;

    /**
     * The size is the number of objects used.
     */
    private int size;
    
    public MyArrayList(){
    //	value = new Object[16];
    	this(10);
    }
    public MyArrayList(int size){
    	if(size<0){
    		try {
				throw new Exception();  //手动抛出一个异常。 讲到异常章节再说,先混个眼熟
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    	value = new Object[size];
    }
    
    public int size(){
    	return size;
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public void add(Object obj){
    	value[size] = obj;
    	size++;
    	if(size>=value.length){
    		//装不下了。扩容吧!
    		int newCapacity = value.length*2;
    		Object[] newList = new Object[newCapacity];
//    		System.arraycopy(src, srcPos, dest, destPos, length);
    		
    		for(int i=0;i<value.length;i++){
    			newList[i] = value[i];
    		}
    		
    		value = newList;
    	}
    }
    
    public Object get(int index){
    	rangeCheck(index);
    	
    	return value[index];
    }
    
    public int indexOf(Object obj){
    	if(obj==null){
    		return -1;
    	}else{
    		for(int i=0;i<value.length;i++){
    			if(obj==value[i]){
    				return i;
    			}
    		}
    		return -1;
    	}
    }
    
    public int lastIndexOf(Object obj){
    	if(obj==null){
    		return -1;
    	}else{
    		for(int i=value.length-1;i>=0;i--){
    			if(obj==value[i]){
    				return i;
    			}
    		}
    		return -1;
    	}
    }
    
    public Object set(int index, Object object) {
    	rangeCheck(index);
    	Object old = value[index];
        value[index] = object;
        return old;
    }
    
    public void rangeCheck(int index){
    	if(index<0||index>size-1){    //[0,size-1]
    		try {
				throw new Exception();  //手动抛出一个异常。 讲到异常章节再说,先混个眼熟
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    }
    
    
    public static void main(String[] args) {
		MyArrayList  list = new MyArrayList(2);
		list.add("aaa");
		list.add(new Human("高琪"));
		list.add("bbbb");
		list.add("bbbb");
		list.add("bbbb");
		list.add("bbbb");
		ArrayList list2;
		
		Human h = (Human) list.get(1);
		System.out.println(h.getName());
		System.out.println(list.size());
		
	}
    
}

class Human {
	private String name;

	public Human(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

字符串常用方法

public class Test02 {
	public static void main(String[] args) {
		String str = new String("abcd");
		String str2 = new String("abcd");
		System.out.println(str2.equals(str));   //比较内容是否相等。
		System.out.println(str2==str);  //注意这里是false

		System.out.println(str.charAt(2));
		
		String str3 = "def";
		String str4 = "def";
		System.out.println(str3.equals(str4));
		System.out.println(str3==str4); //注意这里是true 因为这种方式和上边的方式不同 这种是字符串常量 只有一份
		
		System.out.println(str3.indexOf('y')); 
		String s = str3.substring(0);
		System.out.println(s);
		String str5 = str3.replace('e', '*');
		System.out.println(str5);
		
		String str6 = "abcde,rrtt,cccee";
		String[] strArray = str6.split(",");
		for(int i=0;i<strArray.length;i++){
			System.out.println(strArray[i]);
		}
		String str7 = "  aa  bb  ";
		String str77 = str7.trim();
		System.out.println(str77.length()); 
		
		System.out.println("Abc".equalsIgnoreCase("abc"));
		System.out.println("Abcbd".indexOf('b')); 
		System.out.println("Abcbd".lastIndexOf('b')); 
		System.out.println("Abcbd".startsWith("Ab")); 
		System.out.println("Abcbd".endsWith("bd")); 
		System.out.println("Abcbd".toLowerCase()); 
		System.out.println("Abcbd".toUpperCase()); 
		
		System.out.println("##################");
		String gh = new String("a");
		for (int i = 0; i < 1000; i++) {
			gh = gh + i;
		}
		System.out.println(gh); 
			
	}
}

/**
 * 测试可变字符序列。StringBuilder(线程不安全,效率高),StringBuffer(线程安全,效率低)
 * String:不可变字符序列
 * @author dell
 *
 */
public class Test01 {
	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder();   //字符数组长度初始为16
		StringBuilder sb1 = new StringBuilder(32);   //字符数组长度初始为32
		StringBuilder sb2 = new StringBuilder("abcd");   //字符数组长度初始为32, value[]={'a','b','c','d',\u0000,\u0000...}
		sb2.append("efg");
		sb2.append(true).append(321).append("随便");   //通过return this实现方法链.
		
		System.out.println(sb2);
		
		System.out.println("##################");
		
		StringBuilder gh = new StringBuilder("a");
		for (int i = 0; i < 1000; i++) {
			gh.append(i);
		}
		System.out.println(gh);
		
		
		
		
	}
}

	Integer  d = 1234;
		Integer  d2 = 1234;
		
		System.out.println(d==d2);          //false
		System.out.println(d.equals(d2));  //true
		
		System.out.println("###################"); 
		Integer d3 = -100;    //[-128,127]之间的数,仍然当做基本数据类型来处理。
		Integer d4 = -100;
		System.out.println(d3==d4);      //true
		System.out.println(d3.equals(d4));  //true


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值