尚硅谷0722

课前复习
异常:
package dom1;
public class code1 {
	public static void main(String[] args) {
		int a[][]=new int [4][];
		System.out.println(a[0]);//null
		a[0]=new int [3];
		System.out.println(a[0]);//[I@1ff0dde
		//数组中出现的为异常
		//1.ArrayIndexOutOfBoundsException 
		//2.NullPointerException
		int [] arr = new int[10];
//		for( int i=0 ;i<=arr.length ;i++ ){
//			arr[i] = i;
//		}
//		arr[10];
//		arr[-1];
		int [] arr2=new int [10];
		arr2 = null;
		//arr2 [0] =0;
		
		int  [] arr3 =new int [3];
		//arr3[0]="f";
		System.out.println(arr3[0]);
		
		String  [] arr33 =new String [3];
		//arr3[0]="f";
		//System.out.println(arr33[0].toString());
		
		int [][]arr4 = new int [4][];
		//arr4[0]=new int [3];
		//arr4[0][0]=12;
		System.out.println(arr4);
	}
}
2.面向对象
public textCar{
	public static void main(String [] args){
		Car c1 = new Car();
		c1.name;
		c1.wheel=4;
		c1.display();
		c1.show();			
		2.NullPointerException
		Car c2 = null;
		c2.run();//编译可以通过,但执行报异常
	}
}
class Car{
	String name;
	int wheel;
	public void show(){
		System.out.println("我是一辆车")	
	}
	public void display(){
		System.out.println("name"+name+"age"+age+"wheel"+wheel);
	}
	public void run(){
		System.out.println("汽车启动");	
	}
	
}

3.数组的常见算法
 1.求数组的最大值 最小值 总和 平均数

 2.数组的反转和复制

 3.数组的排序

\
package dom1before;

public class code1 {
	public int getMax(int [] arr){
		int max = arr[0];
		for(int i=0 ;i<arr.length ;i++ ){
			if(max<arr[i]){
				max=arr[i];
			}
		}
		return max;
	}
	public int getMin(int [] arr){
		int min = arr[0];
		for(int i=0 ;i<arr.length ;i++ ){
			if(min>arr[i]){
				min=arr[i];
			}
		}
		return min;
	} 
	public int getSum(int [] arr){
		int sum = 0;
		for(int i=0 ;i<arr.length ;i++ ){
			sum+=arr[i];
		}
		return sum;
	}
	public double getAve(int [] arr){
		int sum = getSum(arr);
		return sum*1.0/arr.length;
	}
	public void printArry(int [] arr){
		System.out.print("{");
		for(int i=0 ;i<arr.length ;i++ ){
			if(i==arr.length-1){
				System.out.println(arr[i]+"}");
				break;
			}
			System.out.print(arr[i]+",");
		}
		System.out.println();
	}
	public void reverArray(int [] arr){
		for(int i=0,j=arr.length-1 ;i<j ;i++,j-- ){
			swp(arr , i ,j);
		}
		System.out.println("颠倒完成..");
	}
	public int [] copy(int [] arr){
		int a []=new int[arr.length];
		for(int i=0 ;i<arr.length ;i++ ){
			a[i]=arr[i];
		}
		System.out.println("拷贝完成");
		return a;
	}
	public int [] sort(int [] arr , String desc){
		if(desc.equals("ascend")){
			for(int i=0 ;i<arr.length ;i++ ){
				for(int j=i ;j<arr.length-1 ;j++ ){
					if(arr[i]>arr[j]){
						swp(arr , i ,j);
					}
				}
			}
			return arr;
		}else if(desc.equals("descend")){
			for(int i=0 ;i<arr.length-1 ;i++ ){
				for(int j=i ;j<arr.length;j++ ){
					if(arr[i]<arr[j]){
						swp(arr , i ,j);
					}
				}
			}
			return arr;
		}else{
			System.out.println("输入错误..");
			return arr;
		}
	}
	public void swp(int [] arr ,int i ,int j ){
		int temp = arr[j];
		arr[j] = arr[i];
		arr[i] = temp;
	}
}

//
package dom1before;
import dom1before.code1;
public class code2 {
	public static void main(String[] args) {
		int a[]=new int []{1 , 4 , 3 , 2 , 5 ,9 , 6 , 7 , 8 , 10 };
		code1 c = new code1();
		c.printArry(a);
		System.out.println("最大值:"+c.getMax(a));
		System.out.println("最小值:"+c.getMin(a));
		System.out.println("求和:"+c.getSum(a));
		System.out.println("最平均值:"+c.getAve(a));
		c.reverArray(a);
		c.printArry(a);
		int s[]=c.copy(a);
		c.printArry(s);
		System.out.println("从大到小:");
		c.printArry(c.sort(a, "ascend"));
		System.out.println("从小到大:");
		c.printArry(c.sort(a, "descend"));
	}
}

/*
 * 类的元素
 * 1.属性(成员变量 Field)
 * 	成员变量 vs局部变量
 * >相同点:声明的格式是类似的 数据类型 变量名 = 初始化值 作用域 都在其定义的作用{}
 *  
 * >不同点:1.声明的位置不同 成员变量:声明在类的内部方法的外部 局部变量 方法内 参数的形参 代码块
 * 		   局部变量前不能放置任何访问修饰符 (private,public,和protected)。
 * 		  final可以用来修饰局部变量 final不叫权限修饰符
 * 		  2.对于成员变量可以
 * 		  3.对于成员变量,可以显式初始化,也可以默认初始化
 * 			byte short int long =>0 float double =>0.0 
 * 			对于局部变量必须显式赋值
 * 		  4在内存分布上不同 成员变量:存放在堆空间;局部变量:存放在栈空间
 * 2方法(函数Meyhod)---封装了一定的功能
 * 		<1. 如何定义一个类的方法 权限修饰符 返回值得类型 方法名(形参列表){
 * 				方法体;
 * 				}
 * 		public void eat(){}
 * 		public void addAgg(int age){}
 * 		public String display(){}
 * 		>1)权限修饰符 private protected public
 * 		>2)返回值类型  没有返回值 (方法声明出使用void) 有具体的返回值的类型 在方法体内指明返回值类型
 * 		void 与return
 * 		满足标识符的命名规范剑灵知意
 * 		参数列表 局部变量 可以根据需要定义一个或者多个
 * 		方法体 通过对象方法的形式进行调用
 * 		方法可以通过“对象,方法”的形式进行调用
 * 		方法内可以调用本类的其他属性和方法
 * 		类的内部可以直接调用类内部的属性
 * 		方法里 不能在定义方法
 * 
 */
package note;
public class opp1zoo {
	public static void main(String[] args) {
		Animal a1 = new Animal();
		a1.show();
		a1.name = "Tom";
		a1.age = 34;
		a1.show();
	}
}
class Animal{
	//1.属性
	String name;
	int age;
	//2.方法
	public void eat(){
		final int a=8 ;
		System.out.println(a+name+"吃饭");
	}
	public void sleep(){
		System.out.println(name+"睡觉");
	}
	public void show(){
		System.out.println("name"+name+"age"+age);
	}
}

变量的分类
按照数据类型分:(byte short int long float double char boolean)vs (类 数组 接口)
按照在类中声明的位置不同:成员变量 局部变量
面向对象的编程思想的实现
1>	创建一个java 类结构(属性 方法 构造器)
2>	创建类对象(java的实例化)
3>	通过(对象.属性)或(对象.方法)的形式 调用类的结构完成相应的功能
4>	创建不同的对象,各自用有一套类的成员副本
5>	创建java类对象的内存解析
6>	内存结构:栈空间(stack)数组的引用,对象的引用,局部变量
7>	对空间(heap)new出来的“东西”数组的实体,对象的实体,包对象的成员变量
8>	方法区:字符串常量池

/*

 * 实验  

1、将对象作为参数传递给方法。

题目要求:

1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积。

2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:

publicvoid printAreas(Cirlce c, int time)

printAreas方法中打印输出1time之间的每个整数半径值,以及对应的面积。例如,times5,则输出半径12345,以及对应的圆面积。

main方法中调用printAreas()方法,调用完毕后输出当前半径值。程序运行结果如图所示。

 

 */

packagecom.atguigu.note;

public class Circle {

    double radius;

    public double findArea(){

        return Math.PI*this.radius*this.radius;

    }

}

packagecom.atguigu.note;

importcom.atguigu.note.Circle;

public class PassObject {

    public void printAreas(Circle c, int time){

        System.out.println("Radius\t\tArea");

//      for(int i=1 ; i<=time ;i++){

//          c.radius=i;

//          System.out.print(c.radius+"\t\t");

//          System.out.print(c.findArea()+"\n");

//      }

        int i=1;

        while(i<=time){

            c.radius=i;

            System.out.print(c.radius+"\t\t");

            System.out.print(c.findArea()+"\n");

            i++;

        }

        c.radius=i;

        System.out.println("\t"+c.radius);

    }

    public static void main(String[] args) {

        PassObject t = new PassObject();

        Circle c = new Circle();

        t.printAreas(c, 5);

    }

}

 

packagedom1before;

public class code3{

    public static void main(String [] args){

        Car c1 = new Car();

        c1.name="baobao";

        c1.wheel=4;

        c1.display();

        c1.show();         

        //2.NullPointerException

        Car c2 = null;

        c2.run();//编译可以通过,但执行报异常

        //匿名对象

        new Car().run();

        new Car().wheel = 6;

        new Car().display();

        //匿名对象的使用

        Factory f = new Factory();

        f.show(c1);

        f.show(new Car());

    }

}

class Factory{

    public void show(Car c){

        c.run();

        c.display();

    }

}

class Car{

    String name;

    int wheel;

    int age=10;

    public void show(){

        System.out.println("我是一辆车");

    }

    public void display(){

        System.out.println("name"+name+"age"+age+"wheel"+wheel);

    }

    public void run(){

        System.out.println("汽车启动"); 

    }

   

}

 

packagecom.atguigu.note;

/*

 * 方法的重载(overload load...

 * 满足两同一不同 同一各类中,同一个方法名参数列表不同(参数个数,参数种类)

 * 1.不能靠形参的参数名称区分不同的方法

 * 2.重载与否,与方法的返回值类型没有关系

 *

 */

 

public class code1 {

    public static void main(String[] args) {

        code1 t =new code1();

        t.getSum(1, 2);

        t.getSum(1, 2);

        t.getSum("qq", "ww");

    }

    public void getSum(int i , int j){

        System.out.println("和为"+(i+j));

    }

    public void getSum(double i , double j){

        System.out.println("总和为"+(i+j));

    }

    public void getSum(String i , String j){

        System.out.println("总和为"+(i+j));

    }

    public void getSum1(String i , String j){//不是重载

        System.out.println("总和为"+(i+j));

    }

    public void getSum(double i ,int j ,double z){

        System.out.println("总和为"+(i+j+z));

    }

/*  private static void getSum(double i1 , doublej1){

        System.out.println("总和为"+(i+j));

    }

*/

    public double getSum(int i , double j ,double z){

        return i+j+z;

    }

    //如下的两个方法构成重载

    public void m1(int i,String str){

       

    }

   

    public void m1(String str,int i){

       

    }

}


/*

 * 编写程序,定义三个重载方法并调用。方法名为mOL

 * 三个方法分别接收一个int参数、两个int参数、一个字符串参数。

 * 分别执行平方运算并输出结果,相乘并输出结果,输出字符串信息。

 * 在主类的main ()方法中分别用参数区别调用三个方法。

 * 3.定义三个重载方法max(),第一个方法求两个int值中的最大值,

 * 第二个方法求两个double值中的最大值,

 * 第三个方法求三个double值中的最大值,并分别调用三个方法。

 *

 */

package com.atguigu.note;

 

public class code2 {

    public static void main(String[]args) {

       

    }

    public void mOl(int i,int j){

       

    }

    public void mOl(String i1){

        System.out.println(i1);

    }

    public void max(int i , int j){

        int max = (i>j) ? i : j;

    }

    public void max(double i , double j){

        double max = (i>j) ? i : j;

    }

    public void max(double i ,double j ,double k){

        double max = (i>j) ? i : j;

        max=(max>k) ? max: k;

    }

}

 

 

再谈方法(method)

什么是方法(函数)?

方法是类或对象行为特征的抽象,也称为函数。

Java里的方法不能独立存在,所有的方法必须定义在类里。                 

  修饰符 返回值类型 方法名(参数类型 形参1,参数类型 形参2,….){

  程序代码

  return 返回值;

其中:

形式参数:在方法被调用时用于接收外部传入的数据的变量。

参数类型:就是该形式参数的数据类型。

返回值:方法在执行完毕后返还给调用它的程序的数据。

返回值类型:方法要返回的结果的数据类型。

实参:调用方法时实际传给函数形式参数的数据。

如何理解方法返回值类型为void的情况 ?

方法的调用

方法只有被调用才会被执行

 

方法调用的过程分析

 

方法的重载(overload)   

重载的概念

在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。

重载的特点:

与返回值类型无关,只看参数列表且参数列表必须不同。(参数个数或参数类型)。调用时,根据方法参数列表的不同来区别。

重载示例:

//返回两个整数的和

  intadd(int x,int y){return x+y;}

//返回三个整数的和

  intadd(int x,int y,int z){return x+y+z;}

//返回两个小数的和

 double add(double x,double y){return x+y;}

 

函数的重载
public class PrintStream{

public static void print(int i) {……}

public static void print(float f) {……}

private static void print(String s) {……}

       publicstatic void main(String[] args){ 

              print(3);

              print(1.2f);

              print(“hello!”);

       }

}

判断

/*

 *

 * 与构成重载的有: void show(int x,chary,double z){}   //no

 * int show(int a,double c,charb){}   //yes

 * boolean show(int c,char b){}  //yes

 * void show(double c){}  //yes

 * double show(int x,char y,doublez){}  //no

 * void shows(){double c}  //no

 *

 * 编写程序,定义三个重载方法并调用。方法名为mOL

 * 三个方法分别接收一个int参数、两个int参数、一个字符串参数。

 * 分别执行平方运算并输出结果,相乘并输出结果,输出字符串信息。

 * 在主类的  main() 方法中分别用参数区别调用三个方法。

 * 定义三个重载方法max(),第一个方法求两个int值中的最大值.

 * 第二个方法求两个doubule值中的最大值,第三个方法求三个double值中的最大值,并分别调用三个方法。

 *

 */

packagecom.atguigu.review;

public class code1 {

    public static void main(String[] args) {

       

    }

    void show(int a,char b,double c){

       

    }

    void show(int a,double c,char b){

       

    }

    boolean show(int c,char b){

        boolean a=false;

        return a;

    }

//  int show(int a,double c,char b){

       

//  }

    void show(double c){

       

    }

//  double show(int x,char y,double z){

       

//  }

    void shows(){double c;}    

}

递归方法

方法递归:一个方法体内调用它自身。

方法递归包含了一种隐式的循环,它会重复执行某段代码,但这种重复执行无须循环控制。

例题:已知有一个数列:f(0)= 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),其中n是大于0的整数,求f(10)的值。

递归一定要向已知方向递归,否则这种递归就变成了无穷递归,类似于死循环。

练习:已知一个数列:f(20)= 1,f(21) = 4,f(n+2) = 2*f(n+1)+f(n),

     其中n是大于0的整数,求f(10)的值。

方法的参数传递

package com.atguigu.note;

public class code5 {

    public static void main(String[] args) {

        code5 t = new code5();

        Data d = new Data();

        t.swap(d);

        System.out.println("m:"+d.m+"n:"+d.n);

        System.out.println("m:"+d.m+"n:"+d.n);

    }

    public void swap(Datad){

        int temp = d.m;

        d.m = d.n;

        d.n = temp;

    }

}

class Data{

    int m = 10 ;

    int n = 5;

}

方法,必须有其所在类或对象调用才有意义。若方法含有参数:

形参:方法声明时的参数

实参:方法调用时实际传给形参的参数值

Java的实参值如何传入方法呢?

 Java里方法的参数传递方式只有一种:值传递。  即将实际参数值的副本(复制品)传入方法内,而参数本身不受影响。


package com.atguigu.note;

/*

 * 1.形参:方法声明时的参数

 *  实参:当通过对象调用方法时,需要给形参传递真时的值

 * 2.若参数是其本身的数据类型的变量,传递的是基本数据类型的变量

 *  若参数引用的数据类型的变量,传递的是基本数据类型的变量法的存储值

 *  若参数是引用数据类型的变量,传递的是引用变量记录的在堆空间传递的首地址值

 *  2.总结:java里方法的参数传递方式只有一种:值传递

 *  即将实际参数值的副本(复制品)传入方法内,

 *     而参数本身不受影响。

 */

public class code4 {

    public static void main(String[] args) {

        int m = 10;

        int n = 5;

        System.out.println("m:" + m +"n:" +n);

        int temp =m;

        m=n;

        n=temp;

        System.out.println("m:" + m +"n:" +n);

        code4 a = new code4 ();

        a.swp(m, n);

    }

    public void swp(int m , int n){

        int temp = m;

        m = n;

        n = temp;

        System.out.println("moth:m:" + m +"n:" +n);

    }

}

方法的参数传递
                     —基本数据类型的参数传递

packagecom.atguigu.note;

public class code5 {

    public static void main(String[] args) {

        code5 t = new code5();

        int a[]=new int []{1,2,3,4,5,6,3,8,8,6,5};

        t.printArr(a );

        t.swap(a,1 ,4);

        t.printArr(a );

        t.swap(a[1] ,a[4]);

        t.printArr(a );

    }

    public void swap(int a [] , int i,int j){

        int temp = a[i];

        a[i] = a[j];

        a[j] = a[i];

    }

    public void swap(int i,int j){

        int temp = i;

        i = j;

        j = i;

    }

    public void printArr(int a[]){

        for(int aa:a){

            System.out.print(aa+" ");

        }

        System.out.println();

    }

}

/*

 * 1 2 3 4 5 6 3 8 8 6 5

 * 1 5 3 4 5 6 3 8 8 6 5

 * 1 5 3 4 5 6 3 8 8 6 5

 */

 

 

package com.atguigu.note;

public class code5 {

    public static void main(String[] args) {

        code5 t = new code5();

        Data d = new Data();

        t.swap(d);

        System.out.println("m:"+d.m+"n:"+d.n);

        System.out.println("m:"+d.m+"n:"+d.n);

    }

    public void swap(Datad){

        int temp = d.m;

        d.m = d.n;

        d.n = temp;

    }

}

class Data{

    int m = 10 ;

    int n = 5;

}





packagecom.atguigu.java;

 

class Value {

       int i = 15;

}

 

public classTestValue {

       public static void main(String argv[]) {

              TestValue t = new TestValue();

              t.first();

       }

 

       public void first() {

              int i = 5;

              Value v = new Value();

              v.i = 25;

              second(v, i);

              System.out.println(v.i);

       }

 

       public void second(Value v, int i) {

              i = 0;

              v.i = 20;

              Value val = new Value();

              v = val;

              System.out.println(v.i + "" + i);

       }

}



/*

主方法里 a=6 b=9

方法里的a的值是9 b6

主方法里 a=6 b=9

*/

方法的参数传递

packagecom.atguigu.review;

public class CODE3 {

    public static void main(String[] args) {

        DD d= new DD();

        d.a = 6;

        d.b = 9;

        swap(d);

        System.out.println("交换后 , a 的值是 "+d.a+";b Field的值是"+d.b);

    }

    public static void swap(DD d){

        int temp = d.a;

        d.a=d.b;

        d.b=temp;

        System.out.println("swp 方法里 aField 的值是"+d.a+"b Field的值"+d.b);

    }

}

/*********************************/

class DD{

    public int a;

    public int b;

}

 

package com.atguigu.review;

 

public class code4 {

    public static void main(String[] args) {

       Value t = new Value();

       t.first();

    }

}

class Value{

    int i=15;

    public void first(){

       int i = 5;

       Value v =new Value();

       v.i = 25;

       second(v,i);

       System.out.println( v.i);

    }

    public void second (Value v , int i){

       i=0;

       v.i = 20;

       Value val = new Value();

       v = val;

       System.out.println(v.i +" " + i);

    }

}

 

信息的封装和隐藏

Java中通过将数据声明为私有的(private),再提供公共的(public)方法:getXxx()和setXxx()实现对该属性的操作,以实现下述目的:

隐藏一个类中不需要对外提供的实现细节;

使用者只能通过事先定制好的方法来访问数据,可以方便地加入控制逻辑,限制对属性的不合理操作;

便于修改,增强代码的可维护性;

 package com.atguigu.review;

class Animal{

    private int legs;

    public void setLegs(int i){

       if (i!=0 && i!=2 &&i!=4) {

           System.out.println("wrong num of");

           return;

       }

       legs=i;

    }

    public int getLegs(){

       return legs;

    }

}

public class code5 {

    public static void main(String[] args) {

       Animal xb = new Animal();

       xb.setLegs(4);

       System.out.println(xb.getLegs());

    }

}

 

/**********************************/

/**

 * 面向对象特征一:封装性

 * 1。问题的提出:当我们直接通过对象.属性的方式。

 * 对对象进行操作,有时会导致数据的错误或安全问题

 *

 * 2解决办法1.将类中的属性设置为private

 * 表明此属性出了这个类外就不能被调用

 * 在类的内部提供public方法设置属性值(setter)和获取属性(getter

 *

 *

 */

packagecom.atguigu.note;

public class code6 {

    public static void main(String[] args) {

        Animal11 a1 = new Animal11();

        a1.setLegs(6);

        a1.setName("aaaa");

        a1.display();

    }

}

class Animal11{

    private String name;

    private int leg;

    private void show(){

        System.out.println("我是个小动物!!");

    }

    public void setName(String s){

        this.name=s;

    }

    public void setLegs(int l){

        if(l>0 && l%2==0){

            leg = 1;

        }else{

            System.out.println("输入的数据有误!!");

        }

    }

    public String getName(){

        return this.name;

    }

    public int getLeg(){

        return this.leg;

    }

    public void display(){

        System.out.println("name:"+this.name+" leg:"

                    +this.leg+"");

    }

}

 

/*
 * 创建程序,在其中定义两个类:
 * Person和TestPerson类。定义如下:
    用setAge()设置人的合法年龄(0~130),
    用getAge()返回人的年龄。
    在TestPerson类中实例化Person类的对象b,
    调用setAge()和getAge()方法,体会Java的封装性。
    
 */
package com.atguigu.review;
public class code6 {
	public static void main(String[] args) {
		Person2 p=new Person2();
		p.setAge(25);
		System.out.println(p.getAge());
		
	}
}
class Person2{
	int age;
	public void setAge(int age){
		if(age>=0 && age<=130){
			this.age=age;
		}else{
			System.out.println("错误");
			return;
		}
	}
	public int getAge(){
		return this.age;
	}
	
}
类的成员之三:构造器(构造方法)
构造器的特征 它具有与类相同的名称 它不声明返回值类型。(与声明为void不同) 不能被static、final、synchronized、abstract、native修饰,不能有return语句返回值  构造器的作用:创建对象;给对象进行初始化 如:Order o = new Order();    Person p = new Person(Peter,15); 如同我们规定每个“人”一出生就必须先洗澡,我们就可以在“人”的构造方法中加入完成“洗澡”的程序代码,于是每个“人”一出生就会自动完成“洗澡”,程序就不必再在每个人刚出生时一个一个地告诉他们要“洗澡”了

构造器
语法格式: 修饰符  类名 (参数列表) { 	    初始化语句; }  举 例: package com.atguigu.review;
public class code7 {

}
class Abimal{
	private int leg;
	public Abimal(){
		leg = 4;
	}
	public void setLeg(int i){
		this.leg = i;
	}
	public int getLeg(){
		return this.leg;
	}
}
创建Animal类的实例:Animal  a=new Animal();     //调用构造器,将legs初始化为4。

构造器
根据参数不同,构造器可以分为如下两类: 隐式无参构造器(系统默认提供) 显式定义一个或多个构造器(无参、有参)  注  意: Java语言中,每个类都至少有一个构造器 默认构造器的修饰符与所属类的修饰符一致 一旦显式定义了构造器,则系统不再提供默认构造器 一个类可以创建多个重载的构造器 父类的构造器不可被子类继承  package com.atguigu.review;
/*
 * 1. 在前面定义的Person类中添加构造器,
 * 利用构造器设置所有人的age属性初始值都为18。
 * 2. 修改上题中类和构造器,增加name属性,
 * 使得每次创建Person对象的同时初始化对象的age属性值和name属性值。
 */
public class code7 {

}
class Abimal{
	private int leg;
	String name ;
	public Abimal(){
		leg = 4;
	}
	public Abimal(String a , int b){
		this.name = a;
		this.leg=b;
	}
	public void setLeg(int i){
		this.leg = i;
	}
	public int getLeg(){
		return this.leg;
	}
}
构造器重载	
构造器一般用来创建对象的同时初始化对象。如 class Person{ 	String name; 	int age; 	public Person(String n , int a){  name=n; age=a;} } 构造器重载使得对象的创建更加灵活,方便创建各种不同的对象。 构造器重载举例: public class Person{    public Person(String name, int age, Date d) {this(name,age);…}    public Person(String name, int age) {…}    public Person(String name, Date d) {…}    public Person(){…} } 构造器重载,参数列表必须不同  /*
 * 3.定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下:
    1)可以生成具有特定坐标的点对象。
    2)提供可以设置三个坐标的方法。
    3)提供可以计算该“点”距原点距离平方的方法。
	4.编写两个类,TriAngle和TestTriAngle,
	其中TriAngle中声明私有的底边长base和高height,同时声明公共方法访问私有变量;
	另一个类中使用这些公共方法,计算三角形的面积。
 * 
 */
package com.atguigu.review;

public class code8 {
	public static void main(String[] args) {
		point p = new point(2,3,4);
		System.out.println(p.geDreit());
		TriAngle a = new TriAngle(3,4);
		System.out.println(a.getArea());
	}
}
class point{
	private int x;
	private int y;
	private int z;
	public point(){}
	public point(int x ,int y ,int z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
	public void setX(int x){
		this.x=x;
	}
	public void setY(int y){
		this.y=y;
	}
	public void setZ(int z){
		this.z=z;
	}
	public int geDreit(){
		return x*x+y*y+z*z;
	}
}
class TriAngle{
	int a;
	int b;
	public TriAngle(){}
	public TriAngle(int a,int b){
		this.a = a;
		this.b = b;
	}
	public void setA(int a){
		this.a = a;
	}
	public void setB(int b){
		this.b = b;
	}
	public double getArea(){
		return a*b/2.0; 
	}
}

/*
 * 构造器重载举例
 */
class Person3{
	private String name;
	private int age;
	private Date birthDate;
	public Person3(String name ,int age , Date d){
		this.name = name;
		this.age = age;
		this.birthDate=birthDate;
	}
	public Person3(String name , int age ){
		this(name , age , null);
	}
	public Person3(String name , Date d ){
		this(name , 30 , null);
	}
	public Person3(String name){
		this(name , 30);
	}
}

/*
 * 1)定义Person类,有4个属性:String name; int age; String school;  
      String major
	(2)定义Person类的3个构造方法:
	第一个构造方法Person(String n, int a)设置类的name和age属性;
	第二个构造方法Person(String n, int a, String s)设置类的name, age 和school属性;
	第三个构造方法Person(String n, int a, String s, String m)设置类的name, age ,school和major属性;
	(3)在main方法中分别调用不同的构造方法创建的对象,并输出其属性值。
 */
package com.atguigu.review;

public class code10 {
	public static void main(String[] args) {
		new Person4("王琪",32).getMessage();
		new Person4("王琪",22,"辽宁工大").getMessage();
		new Person4("王琪",44,"辽宁工大","学生").getMessage();
	}
}
class Person4{
	String name ;
	int age ;
	String school;
	String major;
	public Person4(String n , int a){
		this(n, a, null);
	}
	public Person4(String n , int a ,String s ){
		this(n , a ,s ,null);
	}
	public Person4(String n , int a ,String s ,String m){
		this.name = n;
		this.age = a;
		this.school = school;
		this.major = m;
	}
	public void getMessage(){
		System.out.println("name:"+this.name+"age:"+this.age+
				"school"+this.school+"Mager:"+this.major);
	}
}
/*
 * name:王琪age:32schoolnullMager:null
	name:王琪age:22schoolnullMager:null
	name:王琪age:44schoolnullMager:学生
 */
关键字—this 在java中,this关键字比较难理解,它的作用和其词义很接近。 它在方法内部使用,即这个方法所属对象的引用; 它在构造器内部使用,表示该构造器正在初始化的对象。   this表示当前对象,可以调用类的属性、方法和构         造器 什么时候使用this关键字呢? 当在方法内需要用到调用该方法的对象时,就用this
\
/*
 * 使用this,调用属性、方法
 *  1.当形参与成员变量重名时,
 *  如果在方法内部需要使用成员变量,
 *  必须添加this来表明该变量时类成员
 *  2.在任意方法内,如果使用当前类的成员变量或成员方法
 *  可以在其前面添加this,增强程序的阅读
 */
package com.atguigu.review;
public class code12 {
	public static void main(String[] args) {
		person4 p = new person4(" ww" ,67);
		p.getInfo();
		p.speak();
	}
}
class person4{
	private String name;
	private int age;
	public person4(String name , int age){
		this.name = name;
		this.age = age;
	}
	public void getInfo(){
		System.out.println("姓名:"+this.name);
		this.speak();
	}
	public void speak(){
		System.out.println("年龄:"+this.age);
	}
}







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值