- 允许同一个类中,多个同名的方法的存在,但要求形参列表不一致。
案例
使用细节:
- 方法名必须相同
- 形参列表:必须不同
- 返回类型:无要求
public class Load {
public static int cal(int a,int b)
{
return a+b;
}
public static double cal(double a, int b)
{
return a+b;
}
public static double cal(int a,double b)
{
return a+b;
}
public static int cal(int a,int b,int c)
{
return a+b+c;
}
public static void main(String[]args)
{
System.out.println(cal(10,20));
System.out.println(cal(1,2,3));
System.out.println(cal(1,1.1));
System.out.println(cal(2.2,3));
}
}
public class OverloadExercise {
public static void main(String[]args)
{
Methods Obj=new Methods();
Obj.m(10);
Obj.m(10,30);
Obj.m("hello cmg");
}
}
class Methods{
public void m(int a)
{
System.out.println("方法一输出的结果"+(a*a));
}
public void m(int a,int b)
{
System.out.println("方法二输出的结果"+(a*b));
}
public void m(String s)
{
System.out.println("方法三:字符串的方法是"+s);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Q9hGaIc-1631068535645)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8f8ed640-048a-482e-b029-b5d558104c2c/Untitled.png)]
class compare{
public int max(int a,int b)
{
int c=a>b?a:b;
return c;
}
public double max(double a,double b)
{
double c=a>b?a:b;
return c;
}
public double max(double a,double b,double c)
{
double tmp=a>b?a:b;
double tmp2=tmp>c?tmp:c;
return tmp2;
}
}
可变参数
Java允许同一个类中多个同名同功能但参数个数不统的方法,封装成一个方法
访问修饰符 返回类型 方法名(数据类型...形参名)
{
}
public class VarParmeter {
public static void main(String[]args)
{
HMethod M=new HMethod();
M.sum(1,2,3,4,5);
}
}
class HMethod{
//可以计算2个数的和,3个数的和,4,5
//可以使用方法重载,可以使用可变参数优化
public int sum(int ...nums)
//int ...接收的是可变参数,类型是int,即可接收多个int
//使用可变参数,可以当作使用来使用
{
System.out.println("接收的个数="+nums.length);
return 0;
}
}
注意事项
- 可变参数的实参可以为0或任意多个
- 可变参数的实参可以为数组
- 可变参数的实质为数组
可变参数练习
分别返回姓名和两门课程成绩(总分),姓名和三门课程成绩(总分),姓名和五门课程成绩(总分)。
public class HspMethod {
public static void main(String[]args)
{
Hsp hm=new Hsp();
System.out.println(hm.show("milan",90,98.5));
}
}
class Hsp{
public String show(String name,double ...scores)
{
double total=0;
for(int i=0;i<scores.length;i++)
{
total+=total+scores[i];
}
return name+scores.length+"门课的总分为"+total;
}
}
作用域
- 主要的变量就是成员变量和局部变量
- 局部变量一般是指在成员方法中定义的变量
- 属性如果不赋值,有默认值
- 属性的定义语法同变量,访问修饰符 属性类型 属性名
- 属性和局部变量可以重名,遵循就近原则
- 在同一个作用域中,比如在同一个成员方法中,两个局部变量不能重名
- 全局变量/属性可以加属性修饰符,局部变量不可以使用加修饰符
构造器
[修饰符] 方法名(形参列表){
方法体
}
- 构造器的修饰符可以默认
- 构造器没有返回值
- 方法名和类名字必须一样
- 参数列表和成员方法一样的原则
- 构造器的调用系统完成
构造方法用于对对象的初始化
- 一个类可以定义多个不同的构造器,即构造器的重载
- 如果没有定义构造器,系统会生成默认无参构造器
this关键字
- 哪个对象调用,this就代表哪个对象
- this关键字可以用来访问类的的属性、方法、构造器
- 用于区分当前类的属性和局部变量
- 访问成员方法的语法:this.方法名(参数列表)
- 访问构造器的语法:this(参数列表),只能在构造器中使用,必须放在第一条语句
- this不能再类定义的外部使用,只能在类定义的方法中使用
public class ComPa {
public static void main(String[]args)
{
P b=new P("SMITH",30);
P c=new P("MARY",30);
System.out.println(b.com(c));
}
}
class P{
String name;
int age;
public P(String name ,int age)
{
this.name=name;
this.age=age;
}
public boolean com(P guy)
{
return this.name.equals(guy.name) && this.age == guy.age;
}
}
实例
定义方法max,并返回double数组的最大值
public class Homework01 {
public static void main(String[]args)
{
double [] arr={4,35.1,25.6,77.2,34,5,67.8,90.8};
Max a=new Max();
System.out.println("arr max:"+" "+a.Maximum(arr));
}
}
class Max {
public Double Maximum(double[] arr) {//包装类对象改进
if (arr!=null&&arr.length > 0) {
double max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
} else {
return null;
}
}
}
- 大写的double可以返回null
实现查找
public class Homework02 {
public static void main(String[]args)
{
String [] arr={"hello","good","puyulong","cmg","xiao"};
A02 fun=new A02();
int Index=fun.Judge(arr,"jianbokai");
if(Index!=-1)
{
System.out.println("index is location at"+" "+Index);
}
else{
System.out.println("can not found");
}
}
}
class A02{
public int Judge(String[] str,String X)
{
int index=-1;
if(str!=null&&str.length>0)
{
for(int i=0;i<str.length;i++)
{
if(X.equals(str[i]))
{
index=i;
}
}
}
return index;
}
}
更新价格
public class BOOK {
public static void main(String[]args)
{
Update fun=new Update("huo",152);
fun.show();
fun.upDateprice();
fun.show();
}
}
class Update{
String name;
double price;
public Update(String name,double price)
{
this.name=name;
this.price=price;
}
public void upDateprice()
{
if(price>150)
{
this.price=150;
}
else if(price>100&&price<150)
{
this.price=100;
}
else{
this.price=price;
}
}
public void show()
{
System.out.println("info:"+this.name+this.price);
}
}
实现数组的拷贝
public class A03 {
public static void main(String[]args)
{
int []arr={1,2,3,4,5,6};
Fun lc=new Fun();
int [] Ne=lc.copy(arr);
System.out.println("after copying");
for(int i=0;i<Ne.length;i++)
{
System.out.print(Ne[i]+" ");
}
}
}
class Fun{
public int[] copy(int [] arr)
{
int [] co=new int [arr.length];
for(int i=0;i<arr.length;i++)
{
co[i]=arr[i];
}
return co;
}
}
简单的算术器
public class A06 {
public static void main(String[] args)
{
cal ca=new cal(2,0);
System.out.println("和="+ca.add());
System.out.println("差="+ca.minus());
System.out.println("积="+ca.mul());
Double RES=ca.chu();
if(RES!=null)
{
System.out.println("商="+ca.chu());
}
}
}
class cal{
double num1;
double num2;
public cal(double num1,double num2)
{
this.num1=num1;
this.num2=num2;
}
public double add()
{
return num2+num1;
}
public double minus()
{
return num1-num2;
}
public double mul()
{
return num1*num2;
}
public Double chu()
{
if(num2==0.0)
{
System.out.println("分母不可以为0");
return null;
}
else{
return num1/num2;
}
}
}
构造器的重载
public class A10 {
public static void main(String[]args)
{
Employee A=new Employee("huo",'m',30,"expert",5000);
Employee B=new Employee("KKo",'m',40);
Employee C=new Employee("expert",5000);
}
}
class Employee{
String name;
char sex;//female and male 分别用f和m代替
int age;
String position;//职位
int salary;
public Employee( String name,
char sex,
int age
,String position
,int salary)
{
this.name=name;
this.sex=sex;
this.age=age;
this.position=position;
this.salary=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 class A13 {
public static void main(String[]args)
{
CirCle c=new CirCle();
PassObject op=new PassObject();
op.printArea(c,5);
}
}
class CirCle{
double radius;
public CirCle()
{
}
public CirCle(double radius)
{
this.radius=radius;
}
public double findArea()
{
return Math.PI*radius*radius;
}
//修改当前圆的班级
public void set(double radius)
{
this.radius=radius;
}
}
class PassObject {
public void printArea(CirCle c, int times)
{
double i=1;
System.out.println("radius"+"\t"+"Area");
for(i=1;i<=times;i++)
{
c.set(i);
System.out.println((double)i+"\t"+c.findArea());
}
}
}