Java语言基础(11)

1 构造方法

  构造方法是一种特殊的方法,只有在创建对象的时候才被调用,用来执行初始化的操作,比如给属性赋值...

  1) 构造方法名字跟类名一致,没有返回值也就没有返回值类型
  2) 格式:
    类名(参数列表){
      方法体
    }
  3) 创建对象的标准格式:
    类名 对象名 = new 构造方法
    案例:Demo1

public class Demo11 {
  public static void main(String[] args) {
	Math2 math = new Math2();
	math.sub(100, 50);
	math.sub(100.1, 2.3);
	math.sub(2.99, 1);
	math.sub(100.1, 100);
  }
}
class Math2{
  void sub(int num1,int num2){
	int result = num1 - num2;
	System.out.println("result = "+result);
  }	
  void sub(double num1,int num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
  void sub(int num1,double num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
  void sub(double num1,double num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
}

public class Demo2 {
  public static void main(String[] args) {
	User user1 = 
		new User("刘备",42,"abc","成都",
				 "bei@126.com");
	user1.show();
	User user2 = 
		new User("黄忠",56,"123456","襄阳",
				 "zhong@163.com");
    user2.show();
  }
}
class User{
//属性
  String name;
  int age;
  String password;
  String address;
  String email;
//构造方法
  User(String name1,int age1,
	 String password1,String address1,
	 String email1){
	name = name1;
	age = age1;
	password = password1;
	address = address1;
	email = email1;
  }
//方法
  void show(){
	System.out.println(name+","+age+","
			+password+","+address+","+
			email);  
  }
}

  4) 如果该类中不编写构造方法,则系统默认提供一个空参的构造方法;反之如果编写了构造方法,则会覆盖系统默认提供的空参的构造方法。
    案例:Demo3

public class Demo3 {
  public static void main(String[] args) {
//类名 对象名 = new 构造方法
	Foo foo = new Foo();
  }
}
//如果该类中没有编写构造方法,则系统默认提供一个空参
//构造方法;如果该类中编写了构造方法,则会覆盖系统
//默认提供的空参的构造方法。
class Foo{
//Foo(){系统默认提供空参(没有参数)的构造方法
//}	
}

public class Demo4 {
  public static void main(String[] args) {
//	Koo koo = new Koo();//默认空参构造方法
//报错,在Koo的内部又编写一个带两个参数的构造方法,
//会覆盖系统提供的空参的构造方法。在Koo内部只有编写
//的带两个参数的构造方法。
	Koo koo2 = new Koo("张三",21);
	
  }
}
class Koo{
  String name;	
  int age;
  Koo(String name1,int age1){
	name = name1;
	age = age1;
  }  
}

public class Demo5 {
  public static void main(String[] args) {
	Box1 box = new Box1(100,20,90);
	box.show();
	box.showTiji();
	Box1 box2 = new Box1(200,100,150);
	box2.show();
	box2.showTiji();
  }
}
class Box1{
//属性
  int length;
  int width;
  int height;
//构造方法
  Box1(int length1,int width1,int height1){
	length = length1;
	width = width1;
	height = height1;
  }	
//方法	
  void show(){
	System.out.println(length+","+width
			+","+height);  
  }
  void showTiji(){
	int result = length * width * height;
	System.out.println("体积是"+result);
  }
}

  5) 在企业中建议,构造方法中参数的名字跟要赋值的属性名最好一致,这样对应关系更加的清楚。
    this.属性名:指代当前的属性
    class Person{
      //属性
      String name;
      int age;
      double salary;
      String address;
      //构造方法
      Person(String name,int age,double salary,
      String address){
        this.name = name;
        // 属性 参数
        this.age = age;
        this.salary = salary;
        this.address = address;
      }
    }
    案例:Demo6

public class Demo6 {
  public static void main(String[] args) {
	Person2 p = 
		new Person2("李白",31,20000.1);
	p.show();
  }
}
class Person2{
//属性:
  String name;
  int age;
  double salary;
//构造方法:
  Person2(String name,int age,
		  			double salary){
	this.name = name;
	this.age = age;
	this.salary = salary;
//	      属性		   参数
  }  
//方法:void show():输出每一个属性值
  void show(){
	System.out.println(name+","+age+","
			+salary);  
  }
}

public class Demo7 {
  public static void main(String[] args) {
	User2 user = 
		new User2("张三丰","abc",
			    99,"道士","13555555555");
	user.showAll();
  }
}
class User2{
//属性
  String name;
  String password;
  int age;
  String job;
  String phone;
//构造方法
  User2(String name,String password,
	    int age,String job,String phone){
	this.name = name;
	this.password = password;
	this.age = age;
	this.job = job;
	this.phone = phone;
  }
//方法	
  void showAll(){
	System.out.println(name+","+password
			+","+age+","+job+","+phone);  
  }
}

public class Demo8 {
  public static void main(String[] args) {
	Manager manager = 
		new Manager("张居正",32,
			2000000.1,1000000.1,"北京");
	manager.show();
	manager.showYearSal();
  }
}
class Manager{
//属性
  String name;
  int age;
  double salary;
  double comm;
  String address;
//构造方法	
  Manager(String name,int age,
		  double salary,double comm,
		  String address){
	this.name = name;
	this.age = age;
	this.salary = salary;
	this.comm = comm;
	this.address = address;
  }	
//方法
  void show(){
	System.out.println(name+","+age+","
			+salary+","+comm+","+address);  
  }	
  void showYearSal(){
	double result = salary*12+comm;
	System.out.println(name+"的年薪是"+
			result);
  }
}

public class Demo9 {
  public static void main(String[] args) {
	Car car =
		new Car("哥的奥迪","666666","内敛奢华",
				  "黑色",2000000000.1);
	car.show();
	car.start();
	car.run();
	car.stop();
  }
}
class Car{
//属性
  String name;
  String no;
  String type;
  String color;
  double price;
  int speed=0;
//构造方法
  Car(String name,String no,String type,
		  String color,double price){
	this.name = name;
	this.no = no;
	this.type = type;
	this.color = color;
	this.price = price;
  }	
//方法	
  void show(){
	System.out.println(name+","+no+","+
			type+","+color+","+price+
			","+speed);  
  }	
  void start(){
	speed = 100;
	System.out.println(name+"启动了");
  }
  void run(){
	System.out.println(name+"在行驶," +
			"当前的速度是"+speed);  
  }
  void stop(){
	speed = 0;
	System.out.println(name+"刹车了," +
			"当前的速度是"+speed);
  }
}

    总结:构造方法是一种特殊的方法,只有在创建对象的时候才被调用,用来执行初始化的操作,比如给属性赋值...构造方法的名字跟类名一致,没有返回值类型。

2 方法重载

  在一个类的内部,方法名相同,参数不同的多个方法。
  案例:Demo10

public class Demo10 {
  public static void main(String[] args) {
	/*
	Math1 math = new Math1();
	math.add3(1.1, 200);
	math.add4(2.2,5.3);
	math.add1(300, 500);
	math.add2(1000, 2.2);
	*/
	Math1 math = new Math1();
	math.add(100, 100);
	math.add(200, 1.66);
	math.add(10.1, 1000);
	math.add(2.6, 3.3);
  }
}
class Math1{
  void add(int num1,int num2){
	int sum = num1 + num2;  
	System.out.println(sum);  
  }	
  void add(int num1,double num2){
	double sum = num1 + num2;
	System.out.println(sum);
  }
  void add(double num1,int num2){
	double sum = num1 + num2;
	System.out.println(sum);
  }
  void add(double num1,double num2){
	double sum = num1 + num2;
	System.out.println(sum);
  }
}

public class Demo11 {
  public static void main(String[] args) {
	Math2 math = new Math2();
	math.sub(100, 50);
	math.sub(100.1, 2.3);
	math.sub(2.99, 1);
	math.sub(100.1, 100);
  }
}
class Math2{
  void sub(int num1,int num2){
	int result = num1 - num2;
	System.out.println("result = "+result);
  }	
  void sub(double num1,int num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
  void sub(int num1,double num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
  void sub(double num1,double num2){
	double result = num1 - num2;
	System.out.println("result = "+result);
  }
}

  1)编写类的方法时候,可以把功能相似的方法起同一个名字,参数要不同,通过参数区分开每一个方法,用户只需要记住一个名字,就可以调用相似功能的多个方法。
  2)参数不同:参数类型或者个数必须有一个不同,跟形参的名字无关。
  3)方法重载的好处:方法重载并没有简化代码的编写,但是用户只需要记住一个名字,就可以调用相似功能的多个方法,简少用户的记忆量,方便用户调用方法。
  4)方法重载属于多态的一种。
    多态:多态是面向对象的编程思想之一,事物在不同的情况下有不同的表现。
    eg:花朵,花生,花心,花钱
  5)Sun公司编写的工具类,经常使用到方法重载,方便用户来访问这些方法,用户只要记住一个名字,就可以调用相似功能的多个方法。
    System.out.println(100);
    System.out.println(1.66);
    System.out.println(false);
    System.out.println('中');
    System.out.println("今天风很大");
    案例:Demo12

public class Demo12 {
  public static void main(String[] args) {
	Kongtiao kt = 
		new Kongtiao("美的","柜式",
				  20000.1,"白色",25);
	kt.show();
	kt.cool();
	kt.cool(5);
	kt.hot();
	kt.hot(3);
  }
}
class Kongtiao{
//属性
  String name;
  String type;
  double price;
  String color;
  int degree;
//构造方法
  Kongtiao(String name,String type,
		  double price,String color,
		  int degree){
	this.name = name;
	this.type = type;
	this.price = price;
	this.color = color;
	this.degree = degree;
  }	
//方法
  void show(){
	System.out.println(name+","+type+","
			+price+","+color+","+degree);  
  }
  void hot(){
	degree++;
	System.out.println(name+"升高1度以后,"
			+"当前的温度是"+degree);
  }
  void hot(int degree){
	this.degree = this.degree + degree;
	System.out.println(name+"升高"+degree
		+"度以后,当前的温度"+this.degree);
  }
  void cool(){
	degree--;
	System.out.println(name+"降低1度以后,"
			+"当前的温度是"+degree);
  }
  void cool(int degree){
	this.degree = this.degree - degree;
	System.out.println(name+"降低"+degree
		+"度以后,当前的温度是"+this.degree);
  }
}

public class Demo13 {
  public static void main(String[] args) {
	Zoo1 zoo = new Zoo1();
	zoo.f1();
	zoo.f2(10);
  }
}
//如果一个方法中的参数名字跟属性一致,在该方法中,
//必须使用this.属性名,来指代当前的属性,如果不加
//this关键字,就是参数
class Zoo1{
//属性  
  int num1 = 100;
//方法
  void f1(){
	System.out.println("属性num1的值是"
								+num1);  
  }
  void f2(int num1){
	num1 = 1000;//参数 
	System.out.println("参数num1的值是"
								+num1);
//this.属性名:指代当前的属性
	System.out.println("属性num1的值是"
							+this.num1);
  }
}
原文地址:https://www.cnblogs.com/KalosOwen/p/8414438.html