方法重载+作用域+构造器+this

java允许同一个类,多个重名方法的存在,但是形参列表不一样,方法重载的前提是方法名相同。

public class Hello{
		public static void main(String[] args){
			Methods M = new Methods();
			int i = M.max(9,8);
			System.out.println(i);
			double j = M.max(9.0,8.9);
			System.out.println(j);
			double k = M.max(9.0,8.9,10.2);
			System.out.println(k);
		}
	}

class Methods{
	public int max(int i,int j){
		// if(i>j){
		// 	return i;
		// }else{
		// 	return j;
		// }
		return i>j?i:j;
	}
	public double max(double i,double j){
		if(i>j){
			return i;
		}else{
			return j;
		}
	}
	public double max(double i,double j,double k){
		if(i<j){
			if(j<k){
				return k;
			}else{
				return j;
			}
		}else{
			if(i<k){
				return k;
			}else{
				return i;
			}
		}
	}
	}

return i>j?i:j;表示的是如果i>j则返回i,否则返回j。

java允许将同一类多个同名同功能但参数个数不同的方法,封装成一个方法。通过可变参数实现。

public int sum(int… nums){}//接收多个参数,可以将nums看成一个是数组,num.length

可变参数可以和普通类型的参数一起放在形参列表,但是可变参数必须在形参列表的最后,一个形参列表中只能有一个可变参数。

除了属性之外的变量都可以视为局部变量,只作用在代码块中。全局变量可以不赋值有默认值,局部变量必须赋值,因为没有默认值。

局部变量可以跟全部变量重名,使用时遵循就近原则,但两个局部变量在一个代码块中不能重名。在代码块执行完后局部变量被销毁,但全局变量保留。

构造器基本语法

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

方法体;

}

构造器没有返回值 构造器名要跟类名相同,构造器只是完成对象的初始化,并不是创建对象。在创建对象时,系统自动调用该类的构造方法,下面Person类里有两个构造器,系统自动选择调用。

public class Hello{
		public static void main(String[] args){
			Person p1 = new Person(50,"鹿晗");//第一个构造器
			// System.out.println(p1.name()+p1.age());
			Person p2 = new Person("鹿晗");//第2个构造器
			// System.out.println(p2.name());
		}
	}

class Person {
	int age;
	String name;
	public Person(int pAge,String pName){
		name = pName;
		age = pAge;
		System.out.println(name+age);
	}
	public Person(String pName){
		name = pName;
		System.out.println(name);
	}
	}

如果没有定义构造器系统会自动生成一个默认无参构造器(也叫默认构造器)一旦定义了一个,默认构造器自动消失,

加载Person,在堆中分配空间,完成默认初始化,再执行显示初始化也就是name跟age的赋值语句,最后进行构造器初始化。

this关键字

this表示的是当前对象,哪个对象调用,this关键字可以用来访问本类的属性,方法,构造器。this用来区分当前类的属性和局部变量,this.方法名(参数列表),访问构造器this(参数列表),只能在构造器里访问另一个构造器。且这条语句必须放在第一条,

public class Hello{
		public static void main(String[] args){
			Person p1 = new Person("鹿晗",50);//第一个构造器
			// System.out.println(p1.name()+p1.age());
			Person p2 = new Person("hhh",60);//第2个构造器
			System.out.println(p1.testPerson(p2));
		}
	}

class Person {
	int age;
	String name;
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	public boolean testPerson(Person p){
		return this.name.equals(p.name)&&this.age==p.age;
	}
	}

作业

1、编写类A01,定义方法max,实现求某个double数组的最大值,并返回

public class Hello{
		public static void main(String[] args){
			A01 ma = new A01();
			double a[] = {89.2,63.5,99.1};
			double k = ma.max(a);
			System.out.println("该数组最大值为"+k);
		}
	}

class A01 {

	public double max(double a[]){
		double max = 0;
		for(int i=0;i<a.length-1;i++){
			max = a[i]>a[i+1]?a[i]:a[i+1];
		}
		return max;
	}
	}

2、编写类A02,定义方法find,实现查找某字符串数组中的元素查找,并返回索引。如果找不到,返回-1

import java.util.Scanner;
public class Hello{
		public static void main(String[] args){
			String a[] = {"lina","lisa","locius","jack"};
			Scanner myScanner = new Scanner(System.in);
			System.out.println("请输入一个字符串");
			String b = myScanner.nextLine();
			A02 com = new A02();
			System.out.println("对应下标为"+com.find(a,b));
		}
	}

class A02 {

	public int find(String a[],String b){
		int k = -1;
		for(int i=0;i<a.length;i++){
			if(a[i].equals(b)){
				k = i+1;
			}
		}
		return k;
	}
}

3、编写类Book,定义方法updatePrice,实现更改某本书的价格,具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变

import java.util.Scanner;
public class Hello{
		public static void main(String[] args){
			Book k = new Book("笑傲江湖",300);
			k.info();
			k.updatePrice();
			k.info();
		}
	}

class Book {
	int price;
	String name;
	public Book(String name,int price1){
		this.name = name;
		this.price = price1;
	}
	public void updatePrice(){
		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);
	}
}

4、

public class Hello{
		public static void main(String[] args){
			A03 a = new A03();
			int[] newarr;
			int[] oldarr = {1,2,4,5};
			newarr = a.copyArr(oldarr);
			for(int i =0;i<newarr.length;i++){
				System.out.print(newarr[i]+"\t");
			}
		}
	}

class A03 {

	public int[] copyArr(int arr[]){
		int[] a = new int[arr.length];
		for(int i = 0;i<arr.length;i++){
			a[i] = arr[i];
		}
		return a;
	}
}

5、

import java.util.Scanner;
public class Hello{
		public static void main(String[] args){
			Circle a = new Circle();
			int d = 10;
			System.out.print("半径为"+d+"圆的周长为"+a.perimeter(d)+
				"\t"+"圆的面积为"+a.area(d));
		}
	}

class Circle {

	public double perimeter(int d){
		return Math.PI*d*2;
	}
	public double area(int d){
		return Math.PI*d*d;
	}
}

6、

import java.util.Scanner;
public class Hello{
		public static void main(String[] args){
			Cale a = new Cale(2,0);
			System.out.println("两数之和为:"+a.he());
			System.out.println("两数之差为:"+a.cha());
			System.out.println("两数之积为:"+a.ji());
			Double div = a.shang();
			if(div!=null) {
				System.out.println("两数之商为:"+a.shang());
			}
		}
	}

class Cale {

	double num1;
	double num2;
	public Cale(double num1,double num2){
		this.num1 = num1;
		this.num2 = num2;
	}
	public double he(){
		return num1+num2;
	}
	public double cha(){
		return num1-num2;
	}
	public double ji(){
		return num1*num2;
	}
	public Double shang(){
		if(num2==0) {
			System.out.println("除数为0不能进行运算");
			return null;
		} else {
			return num1/num2;
		}
	}
}

7、

public class Hello{
		public static void main(String[] args){
			Dog a = new Dog("大壮",'白',7);
			a.show();
		}
	}

class Dog {
	String name;
	char color;
	int age;
	public Dog(String name,char color,int age){
		this.name = name;
		this.color = color;
		this.age = age;
	}
	public void show(){
		System.out.println("名字为"+name+"颜色为"+color+
			"年龄为"+age);
	}
}

8、输出10 9 10

11、public double method(double d1,double d2){……}

12、

class Employee {
	String name;//名字
	char gender;//性别
	int age;//年龄
	String posts;//职位
	double salary;//薪水
	public Employee(String posts,double salary){
		this.posts = posts;
		this.salary = salary;
	}

	public Employee(String name,char gender,int age){
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

	public Employee(String name,char gender,int age,
		String posts,double salary){
		this(name,gender,age);
		this.posts = posts;
		this.salary = salary;
	}
}

 

13、

public class Hello{
		public static void main(String[] args){
			Circle a = new Circle(5.5);
			PassObject b = new PassObject();
			b.printAreas(a,5);
		}
	}

class Circle {
	double radius;//圆的半径
	public Circle(double radius){
		this.radius = radius;
	}
}

class PassObject{
	public void printAreas(Circle c, int times){
		System.out.println("Radius"+"\t"+"Area");
		for(int i=1;i<=times;i++){
			double ares = Math.PI*i*i;
			System.out.println((i+0.0) + "\t"+ares);
		}
	}
}

14、

import java.util.Scanner;
import java.util.Random;
public class Hello{
		public static void main(String[] args){
			int count = 0;
			for(int i=0;i<3;i++){
				Scanner myScanner = new Scanner(System.in);
				System.out.println("请出拳(0-石头)(1-剪刀)(0-布)");
				int peoGussNum = myScanner.nextInt();
				if(peoGussNum>2||peoGussNum<0){
					System.out.println("数字输入错误");
					break;
				}else{
					Tom a = new Tom(peoGussNum);
					a.computerNum();
					System.out.println(a.vscomputer());
					if(a.vscomputer().equals("你赢了")){
						count++;
					}
				}
			}
			System.out.println("你赢了"+count+"次");
		}
	}

class Tom {
	int peoGuessNum;
	int comGuessNum;
	public Tom(int peoGussNum){
		this.peoGuessNum = peoGuessNum;
	}
	public int computerNum(){
		Random r = new Random();
		comGuessNum = r.nextInt(3);//返回0-3的整数
		return comGuessNum;
	}
	public String vscomputer(){
		if(peoGuessNum ==0 && comGuessNum == 1){
			return"你赢了";
		}else if(peoGuessNum ==1&& comGuessNum == 2){
			return"你赢了";
		}else if(peoGuessNum == 2 && comGuessNum ==3){
			return"你赢了";
		}else if(peoGuessNum == comGuessNum){
			return "平手";
		} else {
			return"你输了";
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值