java里的常用包 java学习 Lesson4

java.applet: 包含一些用于创建java小应用程序的类
java.awt: 抽象窗口工具包,包含一些用于编写与平台无关的图形界面(GUI)应用程序的类
java.io:包含一些用于输入输出(I/O)处理的类
java.lang:包含一些java语言的基本类与核心类,如String、Math、Integer、System和Runtime,提供常用的功能,这个包中的所有类是被隐式导入的。
java.net: 包含用于建立网络连接的类,与java.io同时使用完成与网络有关的读写
java.util: 包含一些实用工具类和数据结构类

java中,boolean、byte、short、int、long、char、float、double是基本类型,java只有这八种基本数据类型,其余的都是引用类型。

“==”是比较两个变量的值是否相等,“equals”是比较两个对象变量所代表的对象的内容是否相等。
    String str="abc";
    int i=3;
    float f=1.5f;
    char ch = 'f';
    boolean b=false;

    System.out.println(str1 + i+f+ch+b);

针对String的“+”和“+=”,是java中唯一被重载的操作符;在java中,不允许程序员重载操作符。

String类对象是一个常量对象
String str=“abc”;
str = “def”;//先前的“abc”成垃圾内存
在处理大量字符串的程序中,我们通常用StringBuffer来替代String。

函数调用时:
1>在java中,传参时,都是以传值的方式进行。

2>对于基本数据类型,传递的是数据的拷贝;对于引用类型,传递的引用的拷贝


对象的克隆(clone)
1.为了获取对象的一份拷贝,我们可以利用Object类的clone()发法,并声明为public
2.在派生类中覆盖基类的clone()方法,并声明为public
3.在派生类的clone()方法中,调用super.clone()。
4.在派生类中实现Cloneable接口。

class StringTest {  
  
    public static void change(int x, int y){  
        x = x +y;  
        y = x - y;  
        x = x - y;  
    }  
    public static void change(int[] num){  
        num[0] += num[1];  
        num[1] = num[0] - num[1];  
        num[0] = num[0] - num[1];  
    }  
    public static void change(Point pt){  
        pt.x += pt.y;  
        pt.y = pt.x - pt.y;  
        pt.x = pt.x - pt.y;  
    }  
    public static void main (String[] args) {  
        /*int x = 3; 
        int y = 4; 
        change(x, y); 
        System.out.println(x + " " + y);*/  
        /*int[] num = new int[]{3, 4}; 
        change(num); 
        System.out.println(num[0]+" "+num[1]);*/  
        Point pt = new Point();  
        pt.x = 3;  
        pt.y = 4;  
        change(pt);  
        //System.out.println(pt.x +" "+pt.y);  
        System.out.println(pt);  
        //System.out.println(args[0]);  
        /*if(args.length > 0){ 
            for(int i=0;i<args.length;i++){ 
                System.out.println(args[i]); 
            } 
        }*/  
        /*String str1 = new String("abc"); 
        String str2 = new String("abc"); 
        String str3 = str1;*/  
        /*if(str1 == str2){ 
            System.out.println("str1 == str2"); 
        } 
        else { 
            System.out.println("str1 != str2"); 
        }*/  
        /*if(str1 == str3){ 
            System.out.println("str1 == str3"); 
        } 
        else { 
            System.out.println("str1 != str3"); 
        }*/  
        /*if(str1.equals(str2)){ 
            System.out.println("str1 equals str2"); 
        } 
        else { 
            System.out.println("str1 not equals str2"); 
        }*/  
        /*int i=3; 
        float f=1.5f; 
        char ch = 'f'; 
        boolean b=false; 
        //System.out.println(str1 + i+f+ch+b); 
        StringBuffer sb=new StringBuffer(); 
        sb.append(str1).append(i).append(f).append(ch).append(b); 
        //System.out.println(sb.toString()); 
        sb.delete(4, 8); 
        sb.insert(4, "mybole"); 
        System.out.println(sb);*/  
        /*int[] num = new int[3]; 
        for(int i=0;i<num.length;i++){ 
            System.out.println(num[i]); 
        }*/  
        /*Student[] students; 
        students = new Student[3]; 
        for(int i=0;i<students.length;i++){ 
            System.out.println(students[i]); 
        }*/  
        Professor p = new Professor("Wangwu", 50);
		Student s1 = new Student("zhangsan", 18, p);
		Student s2 = (Student)s1.clone();
		s2.name = "lisi";
		s2.age = 20;
		s2.p.name = "lisi";
		s2.p.age = 30;
		//System.out.println(s1.name + " " + s1.age);
		System.out.println(s1.p.name + " " + s1.p.age);
  
          
    }  
      
      
}  
  
class Professor implements Cloneable{
	String name;
	int age;
	public Professor(String name, int age){
		this.name = name;
		this.age = age;
	}
		public Object clone(){
		Object o = null;
		try{
			o = super.clone();
		}catch(CloneNotSupportedException e) {
			System.out.println(e.toString());
		} 
		return o;
	    }
}
class Student implements Cloneable{
	String name;
	int age;
	Professor p;
	Student(String name, int age, Professor p){
		this.name = name;
		this.age = age;
		this.p = p;
	}
	public Object clone(){
		//Object o = null;
		Student o = null;
		try{
			o = (Student)super.clone();
		}catch(CloneNotSupportedException e) {
			System.out.println(e.toString());
		}
		o.p=(Professor)o.p.clone(); 
		return o;
	}
}  
  
class Point{  
    int x, y;  
    public String toString(){  
        return x+" "+y;  
    }  
} 










posted on 2012-04-19 22:29  java课程设计例子  阅读(99)  评论(0编辑  收藏  举报