java之构造方法

构造方法

构造方法也称构造器,是类的特殊的方法,它的作用是完成新对象的初始化

在创建对象时,系统会自动调用该类的构造器完成对象的初始化

语法

[修饰符] 方法名 (形参列表){
			方法体;
}

注意   
1.构造方法 的修饰符可以默认
2.构造方法没有返回值
3.方法名和类名字必须一样
4.参数列表和成员方法一样的规则
5.构造方法的调用是由系统完成
public  class text{
	
	public static void main(String[] args) {
		Person p1 =new Person("XiaoHui",15);//当创建一个对象,调用构造器对 新对象进行初始化
		System.out.println("p1对象名字是"+p1.name);
		System.out.println("p1对象年龄是"+p1.age);
	}	
}

class  Person {
	 String name;
	 int age;
	 
	 //创建一个构造器
	 //注意 构造器没有返回值,名字必须和类名一样
	 public Person(String Name, int Age) {
		 name = Name;
		 age = Age;
	 }
	
}
public  class text{
	
	public static void main(String[] args) {
		Person p1 =new Person("XiaoHui",15);
		Person p2 =new Person("Tom");
		System.out.println("p1对象名字是"+p1.name+"  "+"p1对象年龄是"+p1.age);
		System.out.println("p2对象名字是"+p2.name+"  "+"p2对象年龄是"+p2.age);
	}	
}

class  Person {
	 String name;
	 int age;
	 
	 //创建两个构造器,第一个只给对象初始名字,第二个初始名字和年龄

	  public Person(String Name) {
		 name = Name;
		
	 }
	  public Person(String Name, int Age) {
		 name = Name;
		 age = Age;
	 }	
}

注意

1.一个类中可以定义多个不同的构造器,即构造器重载,进行不同的初始化

2.构造器名和类名相同,构造器没有返回值

3.构造器是完成对象的初始化,并不是创建对象

4.在创建对象时,如果类中有构造器,系统自动调用构造器,没有就默认指向堆空间的对象地址

5.若有两个以上的构造器也被称为构造器中的重载

6.构造器是不能被调用的,只能被系统 创建对象时调用

7.若类中没有定义构造器,系统会自动给类生成一个默认无参构造器,比如person() {},可以使用javap 指令使字节码反编译成类容易阅读,javap -p computer.class,可以知道类的成员变量和方法

 public  class text{
	public static void main(String[] args) {
		
		computer com = new computer();//这个使用的是默认的无参构造器,若在类中申明构造器,则括号里面必须得遵循新的构造器的参数规则
		int  x = com.jc(3);
		 System.out.println("x="+x);
		
	}
}

class  computer{
	
	public   int     jc(int n) {	
		if( n  == 1) {
			 return 1;	
		}
		else  {
			  return	jc(n - 1)  * n;    
		}	
	} 
}

终端显示如下
C:\Users\hhh\Desktop\java文档>java text.java

C:\Users\hhh\Desktop\java文档>javap text.class
public class text {
  public text();//这个就是无参构造器
  public static void main(java.lang.String[]);
}

C:\Users\hhh\Desktop\java文档>javap computer
Compiled from "text.java"//表示该类是来自text.java文件
class computer {
  computer(); //这个就是无参构造器
  public int jc(int);
}

8.若在类中定义了对象初始化构造器,则覆盖默认的构造器,则不能使用默认的无参构造器,除非显式的定义一下,即在类中重新写一个新的无参构造器 比如写computer() {}


 public  class text{
	public static void main(String[] args) {
		Person p1 = new Person();//调用无参构造器,并初始化对象p1
		System.out.println("p1对象的年龄"+p1.age);
		
		Person p2 = new Person("jack",20);//调用有参构造器,并初始化对象p2
		System.out.println("p2对象的名字"+p2.name+"p2对象的年龄"+p2.age);
	}	
}


class   Person {
	int age;
	String name;
	
		public Person() {	 //创建一个无参的构造器,这样就不怕系统的无参构造器被覆盖了
			age =18;
		}
	   public 	Person(String Name, int Age){//创建一个有参的构造器
			name=Name;
			age=Age;
	   }
}

题目

//定义方法max,求某个double数组的最大值,并返回
 public  class text{
	public static void main(String[] args) {
		T	 t = new T();
		Double res =  t.max(null);
		if( res != null) {
			 System.out.println("n的最大值" +res );
		}	
	}
 }
 
      class  T{
		
		public Double max(double... n) {  //Double 是表示返回一个Double的包装类
		    if(n!= null && n.length>0) { //n数组至少有一个元素
			
				 double  max = n[0];
				 for(int i = 1; i < n.length;i++) {
						if( max < n[i] ) {
							max=n[i];
						}			
				   } 
				return max;				   
             }
			else {
			 	System.out.println("n的输入为空" );
					return null;
			 }
			 
		}   
   }
//定义方法find,求从字符串数组中匹配一个人,并返回
 public  class text {
	public static void main(String[] args) {
		String arr[] = {"aaa","bbb","jack","ddd","eee","fff"};
		Taxon t = new Taxon();
	
	  int	index = t.find("jack",arr);
		 if(index != -1) { //判断返回值是否不等于 -1
			  System.out.println("找到了,是第"+t.find("jack",arr)+"位");
		 }else{
			 System.out.println("没找到");
		 }
	
	}
 }
 
 class  Taxon {
		public int find(String s , String arr[]) {
			if(arr != null){ //判断数组是否等于空
				for(int i=0;i<arr.length;i++) {
					if(s == arr[i]) {
						return i+1;
					}
				 }
				return -1; //如果数组为空,返回index=-1
			}
			else{
				return -1; //如果没有找到,就直接退出
			}
	 }
	 
 }
//定义方法find,求从字符串数组中匹配一个人,并返回
 public  class text {
	public static void main(String[] args) {
		
		
		 /*
		 Taxon t = new Taxon();
		 int price = t.updatePrice(200);
		System.out.println(price);
		*/	
		Taxon t = new Taxon("山海经",200);
		t.info();
		t.updatePrice();
		t.info();
	}
 }
 
 /*
class  Taxon {
	int price;
		public void  updatePrice(int price) {
				if(price>150) {
					this.price = 150;
				}else if(price>100 && price<=150) {
					this.price = 100;
				}else {
					this.price = price;
				}
			
		}
}	
*/	
		
class  Taxon {	
		String name;
		int price;
		public  Taxon(String name,int price) { //创建一个构造器
				this.name = name;
				this.price = price;
		}
		public void  updatePrice() {
			//如果方法中,没有price局部变量,this等价于this.price,建议写上
				if(this.price>150) {
					this.price = 150;
				}else if(this.price>100) {
					this.price = 100;
				}
			
		}
		public  void info() {
			
		System.out.println("这本书>"+"名字"+this.name+"|价格"+this.price);
		}
		
}	
	
//定义一个数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
 public  class text {
	public static void main(String[] args) {
		int oldArr[] = {1,2,3,4,5,6};
		Taxon t =new Taxon();
		t.copyArr(oldArr);
		t.infor(oldArr);	
	}	
 }
	
	class Taxon {
		
			public int []  copyArr( int oldArr[]) {
				int newArr [] = new int [oldArr.length];
				for(int i =0; i < oldArr.length; i++) {
					newArr[i] = oldArr[i]; 
				}
				return newArr;
			}
			public void  infor( int  newArr[]) {
				for(int i =0; i <  newArr.length; i++) {
					System.out.print(newArr[i] +"\t");
				}
			}	
	}			
	
//构造器调用
	public static void main(String[] args) {
	}		
 }


 class Employee {
		String name;
		char sex; /
		 int age;
		 String position; // 职位
		 int salary;  // 工资
		 
		 public Employee(String name,char sex,int age) {
			 this.name = name;
			 this.sex = sex; 
			 this.age = age;
		 }
		 public Employee(String position,int salary) {
			 this.position = position;
			 this.salary = salary; 
		 }
		public Employee(String name,char sex,int age,String position,int salary) { //复用构造器,可以减少赋值
			 this( name, sex, age);
			 //this(String position,int salary); 因为构造器直接互相调用,只能在第一句话.
			 this.position = position;
			 this.salary = salary; 
		 }
	 
 }


打印   
 1.0      PI*r*r
   |         |
 5.0      PI*r*r
 

 public  class text{
	public static void main(String[] args) {
		PassObject  p = new PassObject();
		Circle c = new Circle(times);
		p. printAreas(c,5);
	}
 }
 
 
 class Circle {
		double radius;//半径
	
		public  Circle() {
			
		}
		//此时的构造器是为了封装属性,不能随意更改
		public Circle(radius) {
			this.radius = radius;
		}
		
		
		//返回圆的面积
		public   double    findArea() {	
			return    Math.PI * radius * radius;  //返回面积
		}
		
		public   double  upRadio(double radius) {	
		        this.radius = radius;
					 
				} 		
 }
 
 
 class  PassObject {
	 
		public  void printAreas( Circle  c , int times) {
			
				
				for(int i = 1; i <= times; i++ ) {
					  c.upRadio(i);
					System.out.println((double)i+"\t\t\t"+ c.findArea());
				}
		}
 }
//猜拳游戏
import java.util.Scanner;
import java.util.Random;

 public  class text{
	public static void main(String[] args) {
		 Scanner scanner = new Scanner(System.in);  
		 Game g = new Game();
		
		
	
	
	for(int i = 0;i < 3; i++) {
		 System.out.print("请输入你的第"+(i+1)+"次猜拳[石头0,剪刀1,布2]:\t"); 
		 g.m = scanner.nextInt();  //接收用户的值
		 
		 
		  g.n = g.computerNum();  //调用 电脑随机数字方法
		
		 
		 System.out.print("第"+(i+1)+"局猜拳"+g.m+"  VS  "+ g.n ); 
		  g.startGame();  //调用 猜拳结果方法
		
	}
	 System.out.println("你获胜"+g.count+"局" ); 
	
	}
 }
 
 class Game {
	 int m; //用户
	 int  n;  // 机器
	 int count; //获胜的次数
	
	 //电脑随机生成猜拳数字的方法
		public  int computerNum() {
			  Random r = new Random();
			
			 return  r.nextInt(3);// 表示随机0-2数字
		}
	
	//猜拳结果的方法
		public  void  startGame() { 
			  if(m == n) {
				  System.out.println("平局"); 
			  }
			  else if(m ==0 && n ==1  ) {
				   System.out.println("你赢了"); 
				   count++;
			  }
			    else if(m == 1 && n == 2  ) {
				   System.out.println("你赢了"); 
				     count++;
			  }
			    else if(m == 2 && n == 0  ) {
				   System.out.println("你赢了"); 
				     count++;
			  }
			  else {
				   System.out.println("你输了"); 
			  }	  
		 }
		 
 }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值