Java Se 程序运用整合归纳

1.abstracttest

package abstracttest;
/*
 * 抽象类和抽象方法
 * 抽象类也可以像普通类一样,有构造方法、一般方法、属性,
 * 更重要的是还可以有一些抽象方法,留给子类去实现,而且在抽象类中声明构造方法后,在子类中必须明确调用。
 */

abstract public class Abstract {
	String name ;
	int age ;
	String occupation ;
	
	public Abstract(String name){
		this.name=name;
	}
	public Abstract(String name,int age,String occupation)
	{
	this.name = name ;
	this.age = age ;
	this.occupation = occupation ;
	}
	
	// 声明一抽象方法talk()
	public abstract String talk() ;
}

package abstracttest;

public class ExtendAbstract extends Abstract {

	public ExtendAbstract(String name) {
		super(name);
	}
	
	
	//与一般类相同,在抽象类中,也可以拥有构造方法,但是这些构造方法必须在子类中被调用。
//	public ExtendAbstract(String name,int age,String occupation)
//	{
//	this.name = name ;
//	this.age = age ;
//	this.occupation = occupation ;
//	}

	public ExtendAbstract(String name, int age, String occupation) {
		super(name, age, occupation);
	}


	public String talk() {
		 return "学生——>姓名:"+this.name+",年龄:"+this.age+",职业:	22 "+this.occupation+"!" ;
	}

}

package abstracttest;

public class AbstarctTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExtendAbstract extendabstract=new ExtendAbstract("张三",20,"学生");
		System.out.println(extendabstract.talk());
		
		
	}

}


2.array

package array;

public class ArrayCopy {

	/**
	 * @数组的拷贝
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a1[] = {1,2,3,4,5} ; //声明两个整型数组a1、a2,并进行静态初始化
		int a2[] = {9,8,7,6,5,4,3} ;
		System.arraycopy(a1,0,a2,0,3); // 执行数组拷贝的操作,System.arrayCopy(source,0,dest,0,x):
										//语句的意思就是:复制源数组从下标0开始的x个元素到目标数组,
										//从目标数组的下标0所对应的位置开始存取。
		System.out.print("a1数组中的内容:");
		for(int i=0;i<a1.length;i++) // 输出a1数组中的内容
		System.out.print(a1[i]+" ");
		
		System.out.print("\na2数组中的内容:");
		for(int i=0;i<a2.length;i++) //输出a2数组中的内容
		System.out.print(a2[i] +" ");
		System.out.println("\n数组拷贝完成!");
		
	}

}

package array;

import java.util.Arrays;

public class ArraySort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a[] = {4,32,45,32,65,32,2} ;
		
		System.out.print("数组排序前的顺序:");
		for(int i=0;i<a.length;i++)
	
		System.out.print(a[i]+" ");
		Arrays.sort(a); // 数组的排序方法
		System.out.print("\n数组排序后的顺序:");
		for(int i=0;i<a.length;i++)
		System.out.print(a[i]+" ");
	}

}

package array;

public class ArrayTest {

	/**
	 * @数组
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i,min,max;
		int A[]={74,48,30,17,62}; // 声明整数数组A,并赋初值 
		
		min=max=A[0];
		System.out.print("数组A的元素包括: ");
		for(i=0;i<A.length;i++)
		{
		System.out.print(A[i]+" ");
		if(A[i]>max) // 判断最大值 
		max=A[i];
		if(A[i]<min) // 判断最小值 
		min=A[i];
		}
		System.out.println("\n数组的最大值是:"+max); // 输出最大值 
		System.out.println("数组的最小值是:"+min); //
	}

}

package array;

public class TwoArrayTest {
	private static int ss;
	static int ii;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i,j,sum=0;
		int num[][]={{30,35,26,32},{33,34,30,29},{22,34,24,56}}; // 声明数组并设置初值
		
		for(i=0;i<num.length;i++) // 输出销售量并计算总销售量
		{
			System.out.print("第 "+(i+1)+" 个人的成绩为:");
			System.out.println(i);
			for(j=0;j<num[i].length;j++)
			{
				System.out.print(num[i][j]+" ");
				sum+=num[i][j];
			}
			System.out.println();
		}
		System.out.println("总成绩是 "+sum+" 分!");
	}

}


3.collection

package collection;

import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

public class CollectionDemo {

	/**
	 * Collection操作
	 */
	public static void main(String[] args) {

		//Collection collectionss=(Collection) new List();
		
		//Object[] a=(Object[]) new Object();
		
		
		Collection collection1=new ArrayList();	
		//Collection collection1=new LinkedList();	//创建一个集合对象
		
		System.out.println(collection1.add("333"));
		
		collection1.add("000");												//添加对象到Collection 集合中
		collection1.add("111");
		collection1.add("222");
		
		
		System.out.println("集合collection1 的大小:"+collection1.size());
		System.out.println("集合collection1 的内容:"+collection1);
		collection1.remove("000");											//从集合collection1 中移除掉 "000" 这个对象
		System.out.println("集合collection1 移除 000 后的内容:"+collection1);
		System.out.println("集合collection1 中是否包含000 :"+collection1.contains("000"));
		System.out.println("集合collection1 中是否包含111 :"+collection1.contains("111"));
		
		Collection collection2=new ArrayList();
		collection2.addAll(collection1);								//将collection1 集合中的元素全部都加到collection2中
		System.out.println("集合collection2 的内容:"+collection2);
		collection2.clear();											//清空集合 collection1 中的元素
		System.out.println("集合collection2 是否为空:"+collection2.isEmpty());
		
		//将集合collection1 转化为数组
		Object s[]= collection1.toArray();
		
		//Object s[]= collection1.toArray(Object[] a);
		
		for(int i=0;i<s.length;i++){
		System.out.println(s[i]);
		}

	}

}

package collection;

import java.util.*;

public class IteratorDemo {

	/**
	 * 迭代器
	 */
	public static void main(String[] args) {
		Collection collection = new ArrayList();
		
		collection.add("s1");
		collection.add("s2");
		collection.add("s3");
		
		Iterator iterator = collection.iterator();//得到一个迭代器
		
		while (iterator.hasNext()) {//遍历	
		Object element = iterator.next();
		
		System.out.println("iterator = " + element);
		}
		if(collection.isEmpty())
		System.out.println("collection is Empty!");
		else
		System.out.println("collection is not Empty! size="+collection.size());
		
		Iterator iterator2 = collection.iterator();
		while (iterator2.hasNext()) {//移除元素
		Object element = iterator2.next();
		System.out.println("remove: "+element);
		iterator2.remove();
		}
		
		Iterator iterator3 = collection.iterator();
		if (!iterator3.hasNext()) {//察看是否还有元素
		System.out.println("还有元素:"+iterator.hasNext());
		}
		if(collection.isEmpty())
		System.out.println("collection is Empty!");
		//使用collection.isEmpty()方法来判断
		
		System.out.println(collection.size());
	}

}

package collection;

import java.util.LinkedList;

public class LinkedListDemo {

	/**
	 * LinkedList
	 */
	public static void main(String[] args) {
	
		LinkedList linkedlist=new LinkedList();
		
		linkedlist.addFirst("Bernadine");
		linkedlist.addFirst("Elizabeth");
		linkedlist.addFirst("Gene");
		linkedlist.addFirst("Elizabeth");
		linkedlist.addFirst("Clara");
		System.out.println(linkedlist.get(3));
		System.out.println(linkedlist);
		linkedlist.removeLast();
		linkedlist.removeLast();
		System.out.println(linkedlist);

	}

}

package collection;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorDemo {

	/**
	 * ListIteratorDemo
	 */
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		list.add("ddd");
		
		System.out.println("下标0 开始:"+list.listIterator(0).next());     //next()
		System.out.println("下标1 开始:"+list.listIterator(1).next());
		System.out.println("子List 1-3:"+list.subList(1,3));					//子列表
		
		ListIterator it = list.listIterator();							//默认从下标0 开始
		
		//隐式光标属性add 操作 ,插入到当前的下标的前面
		it.add("sss");
		System.out.println(it.next());
		System.out.println(it.nextIndex());
		System.out.println(it.next());
		while(it.hasNext()){
		System.out.println("Index="+it.nextIndex()+",Object="+it.next());
		}
		
		ListIterator it1 = list.listIterator();
		System.out.println(it1.next());
		it1.set("ooo");
		
		ListIterator it2 = list.listIterator(list.size());//下标
		while(it2.hasPrevious()){
		System.out.println("previous Index="+it2.previousIndex()+",Object="+it2.previous());
		}

	}

}

package collection;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {

	/**
	 *Map
	 */
	public static void main(String[] args) {
		Map map1 = new HashMap();
		Map map2 = new HashMap();
		map1.put("1","aaa1");
		map1.put("2","bbb2");
		map2.put("10","aaaa10");
		map2.put("11","bbbb11");
		
		//根据键 "1" 取得值:"aaa1"
		System.out.println("map1.get(\"1\")="+map1.get("1"));
		
		// 根据键 "1" 移除键值对"1"-"aaa1"
		System.out.println("map1.remove(\"1\")="+map1.remove("1"));
		
		System.out.println("map1.get(\"1\")="+map1.get("1"));
		
		map1.putAll(map2);//将map2 全部元素放入map1 中
		map2.clear();//清空map2
		System.out.println("map1 IsEmpty?="+map1.isEmpty());
		System.out.println("map2 IsEmpty?="+map2.isEmpty());
		
		System.out.println("map1 中的键值对的个数size = "+map1.size());
		System.out.println("KeySet="+map1.keySet());//set
		System.out.println("values="+map1.values());//Collection
		System.out.println("entrySet="+map1.entrySet());
		System.out.println("map1 是否包含键:11 = "+map1.containsKey("11"));
		System.out.println("map1 是否包含值:aaa1 = "+map1.containsValue("aaa1"));

	}

}

package collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Set set1 = new HashSet();
		if (set1.add("a")) {//添加成功
		System.out.println("1 add true");
		}
		if (set1.add("a")) {//添加失败
		System.out.println("2 add true");
		}
		set1.add("000");//添加对象到Set 集合中
		set1.add("111");
		set1.add("222");
		
		System.out.println("集合set1 的大小:"+set1.size());
		System.out.println("集合set1 的内容:"+set1);
		
		set1.remove("000");//从集合set1 中移除掉 "000" 这个对象
		System.out.println("集合set1 移除 000 后的内容:"+set1);
		System.out.println("集合set1 中是否包含000 :"+set1.contains("000"));
		System.out.println("集合set1 中是否包含111 :"+set1.contains("111"));
		
		Set set2=new HashSet();
		set2.add("111");
		set2.addAll(set1);//将set1 集合中的元素全部都加到set2 中
		System.out.println("集合set2 的内容:"+set2);
		set2.clear();//清空集合 set1 中的元素
		
		System.out.println("集合set2 是否为空:"+set2.isEmpty());
		
		Iterator iterator = set1.iterator();//得到一个迭代器
		while (iterator.hasNext()) {//遍历
		Object element = iterator.next();
		System.out.println("iterator = " + element);
		}
		//将集合set1 转化为数组
		Object s[]= set1.toArray();
		for(int i=0;i<s.length;i++){
		System.out.println(s[i]);
		}

	}

}


4.configuration

package configuration;


public class ConfigTest {

	/**
	 * Configuration配置文件测试
	 */
	public static void main(String[] args) {
			String name=new ConfigUtil("config.properties").getValue("name");
			System.out.println(name);

		}

}

package configuration;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

	/** 
 	* 读取properties配置文件
 	*/

public class ConfigUtil {
	
	private Properties propertie;
    private InputStream inputFile;
    
    /**
     * 初始化Configuration类
     */
    public ConfigUtil()
    {
        propertie = new Properties();
    }
    
    /**
     * 初始化Configuration类
     * @param filePath 要读取的配置文件的路径+名称
     */
    public ConfigUtil(String filePath)
    {
        propertie = new Properties();
        try {
            inputFile = this.getClass().getClassLoader().getResourceAsStream(filePath); 
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) {
            System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
            ex.printStackTrace();
        } catch (IOException ex){
            System.out.println("装载文件--->失败!");
            ex.printStackTrace();
        }
    }
   
    /**
     * 重载函数,得到key的值
     * @param key 取得其值的键
     * @return key的值
     */
    public String getValue(String key)
    {
        if(propertie.containsKey(key)){
            String value = propertie.getProperty(key);//得到某一属性的值
            return value;
        }
        else{
            System.out.println("配置文件中不包含该键值对!");
            return "";
        }
    }

}


5.DataType

package DataType;

public class CharTest {

	/**
	 * @char类型
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char ch1 = 97 ;
		char ch2 = 'a' ;
	
		System.out.println("ch1 = "+ch1);
		
		System.out.println("ch2 = "+ch2);
	}

}

package DataType;

public class Default {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		byte num1 = 0;
		short num2 = 0;
		int num3 = 0;
		long num4 = 0L;
		float num5=0.0f;
		double num6=0.0d;
		char num7='\u0000';
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(num3);
		System.out.println(num4);
		System.out.println(num5);
		System.out.println(num6);
		System.out.println(num7);
	}

}

package DataType;

public class DobleAndFloat {

	/**
	 * @
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double num1 = 6.3e64D ; // 声明num1为double,其值为-6.3×1064
		double num2 = 5.34E16d ; // e也可以用大写的E来取代
		float num3 = 7.32E2f ; // 声明num3为float,并设初值为7.32f
		float num4 = 2.0E38f;
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(num3);
		System.out.println(num4);
	}

}

package DataType;

public class Escape {

	/**
	 * @输出双引号等转义字符
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char ch ='\"'; 
		
		System.out.println(ch+"测试转义字符!"+ch);
		System.out.println("\"hello world!\"");
	}

}

package DataType;

public class IntTest {

	/**
	 * @整数数据类型
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long num =2147483648L; // 声明一长整型变量
		System.out.println("num="+num);
		
		
		int x = java.lang.Integer.MAX_VALUE ;
		
		System.out.println("x = "+x);
		System.out.println("x + 1 = "+(x+1));
		System.out.println("x + 2 = "+(x+2L));
		System.out.println("x + 3 = "+((long)x+3));
	}

}


6.file

package file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class CopyFile {

	/**
	 * 文件拷贝和显示文件列表
	 */
	
	public static void main(String[] args) {
		String filePath1="D:/test/1/1.txt";
		String filePath2="D:/test/2/2.txt";
		String filePath3="D:/test/";
		
		try {
			copy(filePath1,filePath2);
			//listFile(filePath3);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
	
	/*
	 * 文件拷贝
	 */
	public static void copy(String srcPath,String desPath) throws Exception{
		String folderPath="D:/test/1/1";
		File folder=new File(folderPath);
		//folder.mkdir();
		
		File srcFile=new File(srcPath);
		File desFile=new File(desPath);
		
	    
		//srcFile.delete();
		
		
//		BufferInputStream bis=new BufferInputStream(srcFile);    //字节流
//		BufferOutputStream bos=new BufferOutputStream(desPath);  //字节流
		
		
		FileInputStream fis= new FileInputStream(srcFile);   //字节流
		InputStreamReader isr=new InputStreamReader(fis);
		BufferedReader br=new BufferedReader(isr);        //字符流
		
		
		FileOutputStream fos= new FileOutputStream(desFile);  //字节流
		OutputStreamWriter ow=new OutputStreamWriter(fos); 
		BufferedWriter bw=new BufferedWriter(ow);     //字符流
		//PrintWriter pw=new PrintWriter(ow);
		
		int c;
		
		while((c=fis.read())!=-1){
			bw.write(c);
		}
		bw.close();
		fos.close();
		
	}
	
	/*
	 * 文件列表,包括文件夹中的文件
	 */
	private static void listFile(String path) throws Exception{
		File file=new File(path);
		File files[]=file.listFiles();
		for(int i=0;i<files.length;i++){
			
			if(files[i].isDirectory()){
				listFile(path+files[i].getName());
			}
			//System.out.println(files[i].getName());
			
			//System.out.println(files[i].toString()); 
			System.out.println(files[i].getPath());
		}
	}

}

package file;

public class Static {
	
	static {
		System.out.println(1111);
	}

	/**
	 * 外部类和主类(main类)执行顺序
	 */
	
	public Static(){
		System.out.println("主类的构造方法");
	}
	
	public static void main(String[] args) {
		S.say();
		new S().say2();
		say3();
	}
	
	static class S{
		public S(){
			System.out.println("S的构造方法");
		}
		static void say(){
			System.out.println("say hello");
		}
		void say2(){
			System.out.println("say hello2");
		}
	}
	
	
	 static void say3(){
		System.out.println("say hello3");
	}

}


7.huiwen

package huiwen;

import java.applet.Applet;
import java.awt.Event;
import java.awt.Label;
import java.awt.TextField;

/**
 * 回文
 * @author Bryant
 *
 */
public class Huiwen extends Applet {
	 Label prompt;

	 TextField input;

	 int data;

	 public void init() {
	  prompt = new Label("输入数字");
	  input = new TextField(4);
	  add(prompt);
	  add(input);
	 }

	 public boolean action(Event e, Object o) {
	  int data = Integer.parseInt(input.getText());
	  showStatus("");
	  input.setText("");

	  int count = 0;
	  String[] arr = new String[1000000];
	  while (data != 0) {
	   arr[count] = String.valueOf(data % 10);
	   data = data / 10;
	   count++;
	  }

	  int j = count - 1;
	  boolean flag = true;
	  for (int i = 0; i < count / 2; i++, j--) {
	   if (!arr[i].equals(arr[j])){
	    showStatus("不是回文数");
	    flag = false;
	   }
	  }

	  if(flag){
	   showStatus("是回文数");
	  }
	  repaint();
	  return true;
	 }
	} 

package huiwen;

import java.applet.Applet;
import java.awt.Event;
import java.awt.Label;
import java.awt.TextField;

/**
 * 回文(比第一个省内存)
 * @author Bryant
 *
 */

public class Huiwen2 extends Applet {

	 Label lable;

	 TextField input;

	 public void init() {
	  lable = new Label("输入数字");
	  input = new TextField(100);
	  add(lable);
	  add(input);
	 }

	 public boolean action(Event e, Object o) {
	  String text = input.getText().trim();
	  showStatus("");
	  input.setText("");

	  StringBuilder sb = new StringBuilder(text);

	  if (sb.reverse().toString().equals(text)) {
	   showStatus("不是回文数");
	  } else {
	   showStatus("是回文数");
	  }
	  repaint();
	  return true;
	 }
	}


8.innerclass

package innerclass;
/*
 * 内部类的使用
 */

public class OuterClass {
	
	int score=100;
	
	void inst(){
		Inner in=new Inner();
		in.display();
	}
	
	public class Inner{  //内部类也可以通过创建对象从外部类之外被调用,只要将内部类声明为public即可
		void display(){
			String name="zhangsan";
			System.out.println("score="+score);//内部类可以直接调用外部类的属性
												
		}
	}
	
	public static class Inner2{
		void display(){
			String name="zhangsan";
			//System.out.println("score="+score);  //用static也可以声明内部类,用static声明的内部类则变成外部类,
												//但是用static声明的内部类不能访问非static的外部类属性。
												
		}
	}
	
	
//	void print(){
//		System.out.println("name="+name);  //外部类不能使用内部类的属性
//	}

}

package innerclass;

public class OuterClassTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		OuterClass out=new OuterClass();
		out.inst();
		
		OuterClass.Inner innner=out.new Inner();
		innner.display();//内部类也可以通过创建对象从外部类之外被调用,只要将内部类声明为public即可

	}

}

package innerclass;
/*
 * 在外部类方法中定义内部类
 */

public class OuterClass2 {
	int score = 95;
	void inst()
	{
		class Inner
		{
			void display()
			{
				System.out.println("成绩: score = " + score);
			}
		}
	Inner in = new Inner();
	in.display();
}
}

package innerclass;

public class OuterClassTest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		{
			OuterClass2 outer = new OuterClass2();
			outer.inst() ;
			}
	}

}

package innerclass;
/*
 * 在方法中定义的内部类只能访问方法中的final类型的局部变量,
 * 因为用final定义的局部变量相当于是一个常量,它的生命周期超出方法运行的生命周期.
 */

public class OuterClass3 {
	int score = 95;
	void inst(final int s)
	{
	final int temp = 20 ;
	class Inner
	{
	void display()
	{
	System.out.println("成绩: score = " + (score+s+temp));
	}
	}
	Inner in = new Inner();
	in.display();
	}
}

package innerclass;

public class OuterClassTest3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		OuterClass3 outer=new OuterClass3();
		outer.inst(5);
	}

}

package innerclass;

public class SetApple {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			apple a=new apple();
			a.appleweight=0.5;
			System.out.println("苹果重1两");
			System.out.println(a.bite());
			
			a.appleweight=5;
			System.out.println("苹果重5两");
			System.out.println(a.bite());
			
		}
	}


	//内部类
	class apple{
		long applecolor;
		double appleweight;
		boolean eatup;
		
		public boolean bite(){
			if(appleweight<1){
				System.out.println("苹果已经吃完了");
				eatup=true;
			}else{
				System.out.println("苹果吃不下了");
				eatup=false;
			}
			return eatup;
		}
	}


package innerclass;

public class TestExtend extends Employee {

	/**
	 * @param args
	 */
	public TestExtend(String name,int salary){
		super(name,salary);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("覆盖的方法调用" + getSalary("张三",500)); 
		System.out.println("继承的方法调用" + getSalary2("李四",500)); 
		System.out.println("覆盖的方法调用" + getSalary("张三",10000)); 
		System.out.println("继承的方法调用" + getSalary2("李四",10000)); 
	}
	
	public static String getSalary(String name, int salary)    //重写父类的方法(覆盖的方法)
	{ 	
	    String str; 
	    if (salary>5000) 
	             str = "名字" + name + "Salary: " + salary; 
	    else 
	             str = "名字" + name + "Salary:低于5000";  
	    return str; 
	} 
	
	public String getSalary3(){
		return getSalary();
	}
	
	
};


class Employee   //父类
{ 
	//public String name;// 
	//public int salary;// 
	
	private String name;//   如果把 变量设为private,即私有化变量,则需要增加构造函数初始化类
	private int salary;// 

	public Employee(String _name,int _salary){
		name=_name;
		salary=_salary;
	}
	
	public static String getSalary(String name, int salary) 
	{ 
           String str; 
           str = "名字" + name + "Salary: " + salary; 
           return str; 
	} 

    public static String getSalary2(String name, int salary) 
     { 
         String str; 
         str = "名字" + name + "Salary: " + salary; 
         return str; 
     } 
    
    public String getSalary(){
    	String str; 
        str = "名字" + name + "Salary: " + salary; 
        return str;
    }
}; 


9.io

package io;

import java.io.*;

public class SystemIo {

	/**
	 * @System.in
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int bytes = 0;
		byte buf[] = new byte[255];
		System.out.print("请输入您的文本:");
		
		try{
			bytes = System.in.read(buf,0,255);
			System.out.print(bytes);
			System.out.println("");
			String inStr = new String(buf,0,bytes);
			System.out.println(inStr);
		}catch(IOException e){
		System.out.println(e.getMessage());
		}
	}

}


10.local

package local;

import java.util.Locale;

public class LocaleList {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//返回Java 所支持的全部国家和语言的数组
		Locale[] localeList = Locale.getAvailableLocales();
		
		//遍历数组的每个元素,依次获取所支持的国家和语言
		for (int i = 0; i < localeList.length ; i++ )
			
		//打印出所支持的国家和语言
		System.out.println(localeList[i] .getDisplayCountry() + "=" +
		localeList[i] .getCountry()+ "   " +
		localeList[i] .getDisplayLanguage() + "=" +localeList[i] .getLanguage());
	}

}


11.main

package main;

public class MainMethod {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int j = args.length ;
		if(j!=2)
		{
		System.out.println("输入参数个数有错误!") ;
		// 退出程序
		System.exit(1) ;
		}
		for (int i=0;i<args.length ;i++ )
		{
		System.out.println(args[i]) ;
		}
	}

}


12.privatetest

package privatetest;

public class Change {
	int x=0;

}

package privatetest;

public class ChangeTest {

	/**
	 * @引用数据类型 传递修改值(改变值,值类型不改变)
	 * 在fun方法中所做的操作,是会影响原先的参数
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Change c = new Change() ;
		c.x = 20 ;
		fun(c) ;
		System.out.println("x = "+c.x);
		}
		
	public static void fun(Change c1)
		{
		c1.x = 25 ;
		}
}

package privatetest;

public class Person {
	String name ;
	int age ;
}

package privatetest;
/*
 * 封装
 */

public class PersonPrivate {
	private String name ;
	private int age ;
	
	private static void talk()
	{
	//System.out.println("我是:"+name+",今年:"+age+"岁");
	}
	
	public void say()
	{
	talk();
	}
	
	public void setName(String str)
	{
	name = str ;
	}
	
	public void setAge(int a)
	{
		if(a>0)
			age = a ;
	}
	
	public String getName()
	{
	return name ;
	}
	
	public int getAge()
	{
	return age ;
		}
}

package privatetest;

public class PersonTest {

	/**
	 * @引用数据类型值传递(改变值)
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 声明一对象p1,此对象的值为null,表示未实例化
		Person p1 = null ;
		
		// 声明一对象p2,此对象的值为null,表示未实例
		 Person p2 = null ;
		 
		// 实例化p1对象
		 p1 = new Person() ;
		 
		// 为p1对象中的属性赋值
		p1.name = "张三" ;
		p1.age = 25 ;
		
		// 将p1的引用赋给p2
		p2 = p1 ;
		
		// 输出p2对象中的属性
		System.out.println("姓名:"+p2.name);
		System.out.println("年龄:"+p2.age);
		//p2=null;
		p1 = null ;
//		System.out.println("姓名:"+p1.name);
//		System.out.println("年龄:"+p1.age);
		
		p2.name="李四";
		System.out.println("姓名:"+p2.name);
	}

}

package privatetest;
/*
 * 构造方法私有化
 */

public class PublicToPrivate {

	private PublicToPrivate()
	{
	System.out.println("private TestSingleDemo1 .") ;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new PublicToPrivate() ;
	}

}


13.randow

package randow;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Randow {

	/**
	 * 生成随机数
	 */
	
	public static void main(String[] args) {
//		int[] ints = new int[100];
//		for(int i=0;i<100;i++) {
//		int temp = (int) (Math.random()*100+1);
//		for(int j=0;j<i;j++) {
//		if(temp==ints[j]) {
//		temp = (int) (Math.random()*100+1);
//		j=0;
//		}
//		}
//		ints[i]=temp;
//		}
//		Arrays.sort(ints);
//		for(int index:ints) {
//		if(index%10==0)
//		System.out.println();
//		System.out.print(index+" ");
//		}

		
		
		//随机生成不重复的两位数
		 String result="";
		 String str[]={"0","1","2","3","4","5","6","7","8","9"};
		 Random r=new Random();
		 while(result.length()<=1){
			 int i=r.nextInt(10);
			 if(result.indexOf(str[i])==-1){
				 result=result+str[i];
			 } 
		 }
		 
		// System.out.println(result);
		

		validateCode(2);
		
		getNum();
		
		random2();
	}
	
	
	 //生产2位随机数
	 
	 public static void validateCode(int code_len) {   
	        int count = 0;   
	        char str[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };   
	        StringBuffer pwd = new StringBuffer("");   
	        Random r = new Random();   
	        while (count < code_len) {   
	            int i = Math.abs(r.nextInt(10));  
	            //int j = Math.abs(r.nextInt(10));
	            if (i >= 0 && i < str.length) {   
	                pwd.append(str[i]);   
	                count++;   
	                
	            }   
	            
	        }   
	       // System.out.println(pwd.toString());
	        String pwd2="";
	        //return pwd.toString();   
	    }

	 
	 
	 /**
		 * 产生两位不重复的随机数
		 * 
		 * @return
		 */
		protected static void getNum() {
			
			Map<String, String> map = new HashMap<String, String>();
			for (int i = 0; i < 99; i++) {
				String key = i < 10 ? "0" + i : i + "";
				map.put(key, key);
			}		
			
			String num = getRandom();
			
			if (map.containsKey(num)) {			
				getNum();
			}else{
				System.out.println(num);
			}
		}	

		public static String getRandom() {
			String str = "";
			Random random = new Random();
			int num = random.nextInt(100);
			if (num < 10) {
				str = "0" + num;
			} else {
				str = num + "";
			}
			//System.out.println(str);
			return str;
		}

		
		
		public static int[] random2(){
			int send[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
			int temp1,temp2,temp3;
			Random r=new Random();
			for(int i=0;i<send.length;i++){
				temp1=Math.abs(r.nextInt())%(send.length-1);
				temp2=Math.abs(r.nextInt())%(send.length-1);
				if(temp1!=temp2){
					temp3=send[temp1];
					send[temp1]=send[temp2];
					send[temp2]=temp3;
				}
				
			}
			//System.out.println(send);
			return send;
		}
		
		
	 
}


14.recursion

package recursion;

public class RecursionTest {

	/**
	 * 递归
	 * @param args
	 */
	
	int sum=0;
	int a=0;
	public void sum(){
		sum+=a;
		a+=1;
		if(a<5){
			sum();
		}
	}
	
	public static void main(String[] args) {
		RecursionTest test=new RecursionTest();
		test.sum();
		System.out.println("计算结果为:"+test.sum);
	}

}


15.statictest

package statictest;
/*
 * static静态变量的使用
 */

public class Person {
	String name ;
	static String city = "中国";
	int age ;
	
	static int count=0 ;
	
	public Person(String name,int age)
	{
	this.name = name ;
	this.age = age ;
	}
	
	public String talk()
	{
	return "我是:"+this.name+",今年:"+this.age+"岁,来自:"+city;
	}
	
	public Person()
	{
	count++ ; // 增加了一个对象
	System.out.println("产生了:"+count+"个对象!");
	talk();
	}
}

package statictest;

public class StaticPersonTest {

	/**
	 * static 静态变量修改的是所有对象的属性
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person p1 = new Person("张三",25) ;
		Person p2 = new Person("李四",30) ;
		Person p3 = new Person("王五",35) ;
		System.out.println("修改之前信息:"+p1.talk()) ;
		System.out.println("修改之前信息:"+p2.talk()) ;
		System.out.println("修改之前信息:"+p3.talk()) ;
		
		System.out.println(" ************* 修改之后信息 **************");
		
		// 修改后的信息
		p1.city = "美国" ;
		p1.name="wangliu";
		//只有静态变量才可以用类名直接调用
		//Person.city="呵呵";
		
		System.out.println("修改之后信息:"+p1.talk()) ;
		System.out.println("修改之后信息:"+p2.talk()) ;
		System.out.println("修改之后信息:"+p3.talk()) ;
		
		new Person().talk();
		//new Person();
	}

}

package statictest;
/*
 * 静态方法的使用(静态方法调用的变量只能是静态的),静态方法调用非静态方法必须实例化
 */

public class Person2 {

	String name ;
	private static String city = "中国";
	int age ;
	
	public Person2(String name,int age)
	{
	this.name = name ;
	this.age = age ;
	}
	
	
	
	public Person2()
	{
	}
	
	
	
	//静态变量既可以在静态方法中使用,也可在非静态方法中使用。
	public String talk()
	{
	return "我是:"+this.name+",今年:"+this.age+"岁,来自:"+city;
		//return null;
	
	//非静态方法不能调用静态方法(错,可以)
	//setCity("");
	}
	
	public void talk2(){
		setCity("");
	}
	
	public static void setCity(String c)
	{
		//静态方法中不能使用this
		//this.city=c;
		
		//静态方法调用的变量只能是静态的
		//name=c;
		
	city = c ;
	
	
	//静态方法调用非静态方法必须实例化
	new Person2().talk();
	}
}

package statictest;

public class StaticPersonTest2 {

	/**
	 * static 静态方法的调用
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person2 p1 = new Person2("张三",25) ;
		Person2 p2 = new Person2("李四",30) ;
		Person2 p3 = new Person2("王五",35) ;
		System.out.println("修改之前信息:"+p1.talk()) ;
		System.out.println("修改之前信息:"+p2.talk()) ;
		System.out.println("修改之前信息:"+p3.talk()) ;
		
		System.out.println(" ************* 修改之后信息 **************");
		
		// 修改后的信息
		//p1.city = "美国" ;
		//只有静态变量才可以用类名直接调用
		Person.city="呵呵";
		Person2.setCity("美国") ;
		
		System.out.println("修改之后信息:"+p1.talk()) ;
		System.out.println("修改之后信息:"+p2.talk()) ;
		System.out.println("修改之后信息:"+p3.talk()) ;
		
		
		//调用类的方法是调用无参的构造方法,或者不写构造方法,直接实例化后调用
		new Person2().talk();
		
	}

}

package statictest;
/*
 * 静态代码块(首先被执行)
 */

public class Person3 {
	public Person3()
	{
	System.out.println("1.public Person()");
	}
	
	// 此段代码会首先被执行
	static
	{
	System.out.println("2.Person类的静态代码块被调用!");
	}
}

package statictest;

/*
 * 静态代码块首先被执行
 */

public class StaticPersonTest3 {

	static
	{
	System.out.println("3.TestStaticDemo5类的静态代码块被调用!");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("4.程序开始执行!");
		 // 产生两个实例化对象
		new Person3() ;
	}

}


16.string

package string;

public class StringDemo {

	/**
	 * 由程序输出结果可以发现,str1与str3相等,这是为什么呢?
	 * 还记得上面刚提到过“==”是用来比较内存地址值的。现在str1与str3相等,则证明str1与str3是指向同一个内存空间的。
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = "java" ;
		String str2 = new String("java") ;
		String str3 = "java" ;
		
		System.out.println("str1 == str2 ? --- > "+(str1==str2)) ;
		System.out.println("str1 == str3 ? --- > "+(str1==str3)) ;
		System.out.println("str3 == str2 ? --- > "+(str3==str2)) ;
	}

}

package string;

public class StringTest1 {

	/**
	 * ==号比较内存地址值是否相同
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = new String("java") ;
		String str2 = new String("java") ;
		String str3 = str2 ;
		
		if(str1==str2)
			{
			System.out.println("str1 == str2");
			}
		else
			{
			System.out.println("str1 != str2") ;
		}
		if(str2==str3)
		{
			System.out.println("str2 == str3");
		}
		else
			{
			System.out.println("str2 != str3") ;
		}
	}

}

package string;

public class StringTest2 {

	/**
	 * equals比较2个对象的内容是否一致
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = new String("java") ;
		String str2 = new String("java") ;
		String str3 = str2 ;
		
		if(str1.equals(str2))
		{
		System.out.println("str1 equals str2");
		}
		else
		{
		System.out.println("str1 not equals str2") ;
		}
		if(str2.equals(str3))
		{
		System.out.println("str2 equals str3");
		}
		else
		{
		System.out.println("str2 note equals str3") ;
		}
	}

}


17.supertest

package supertest;
/*
 * 继承
 */

public class Person {
	String name;
	int age;
	
	public Person()
	{
	System.out.println("1.public Person(){}") ;
	}
	
	public Person(String name,int age)
	{
	this.name = name ;
	this.age = age ;
	}
	
	public void talk(){
		
	}
}

package supertest;

public class Student extends Person {
	String school ;
	 // 子类的构造方法
	public Student()
	{
		super("张三",25);
		
	System.out.println("2.public Student(){}");
	}
}

package supertest;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s = new Student() ;
		s.school="北京";
		System.out.println("姓名:"+s.name+",年龄:"+s.age+",学校:"+s.school);
	}

}

18.test

package test;

public class EqualsTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1="hello";
		String s2="hello";
		System.out.println(s1==s2);
		System.out.println(s1.equals(s2));
		
		String s3=new String("hello");
		String s4=new String("hello");
		System.out.println(s3==s4);
		System.out.println(s3.equals(s4));
		
		char b=97;
		System.out.println(b);
	}

}


19.thistest

package thistest;

/*
 * this 调用构造方法,只能放在首行
 */

public class Person {

	
	String name ;
	int age ;
	public Person()
	{
		System.out.println("1. public Person()");
	}
		
	public Person(String name,int age){
		
	// 调用本类中无参构造方法
	this() ;
	
	this.name = name ;
	this.age = age ;
	System.out.println("2. public Person(String name,int age)");
	}
}

package thistest;

public class ThisPersonTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		new Person("张三",25) ;
	

	}

}


20.thread

package thread;

public class MyThread {

	/**
	 * 主线程
	 */
	public static void main(String[] args) {
		ThreadDemo t1=new ThreadDemo("实例线程1");
		t1.startthread();
		//t1.runthread();
		
		
		ThreadDemo t2=new ThreadDemo("实例线程2");
		t2.startthread();
		//t2.runthread();
		

	}

}

package thread;

public class ThreadDemo extends Thread {
	
	Thread thread;
	
	String str;
	
	//构造函数
	public ThreadDemo(String str1){
		this.str=str1;
	}
	
	//启动线程
	public void startthread(){
		thread=new Thread(this);
		thread.start();
	}
	
	
	public void runthread(){
		int i=0;
		while(thread!=null){
			try{
				if(i==5)
					sleep(10000);
			}catch(Exception e){
				System.out.print(e.getMessage());
			}
			System.out.println(str);
			i++;
		}
	}

}

package thread;

public class TwoThreadDemo extends Thread {
	//String name;

	/**
	 *  静态方法调用非静态方法必须实例化
	 */
	public static void main(String[] args) {
		TwoThreadDemo tt = new TwoThreadDemo();
	        tt.start();
	        for ( int i = 0; i < 3; i++ ) {
	            System.out.println("Main thread");
	        }
	        new TwoThreadDemo().run1();
	        run2();
	}
	
	
//	public TwoThreadDemo(String name){
//		this.name=name;
//	}
	
	 public void run() {
	        for ( int i = 0; i < 3; i++ ) {
	            System.out.println("Run thread");
	        }
	    }
	 
	 public  void run1() {
	        for ( int i = 0; i < 3; i++ ) {
	            System.out.println("New thread");
	        }
	        
	     // run2();
	    }
	 
	 
	 public static void run2(){
		 for ( int i = 0; i < 3; i++ ) {
	            System.out.println("My thread");
	        }
		 
	 }


}

package thread;


//设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1
//以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题
public class ThreadTest{
	  private int j;
	  
	  public static void main(String args[]){
		  ThreadTest tt=new ThreadTest();
		  Inc inc=tt.new Inc();
		  Dec dec=tt.new Dec();
		  for(int i=0;i<2;i++){
			  Thread t=new Thread(inc);
			  t.start();
			  t=new Thread(dec);
			  t.start();
		  }
	  }
	  private synchronized void inc(){
		  j++;
		  System.out.println(Thread.currentThread().getName()+"-inc:"+j);
	  }
	 	private synchronized void dec(){
	 		j--;
	 		System.out.println(Thread.currentThread().getName()+"-dec:"+j);
	 	}
	 class Inc implements Runnable{
		 public void run(){
			 for(int i=0;i<100;i++){
		inc();
			 }
		 }
	  }
	 class Dec implements Runnable{
		 public void run(){
			 for(int i=0;i<100;i++){
				 dec();
			 }
		 }
		  }
		}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值