方法需要修饰符、返回值、方法名称、抛出异常
eg:
public int max(int a,int b) throws IOException{
}
方法的调用:
静态方法 可以直接调用 ,因为静态方法和类是同时存在的,而非静态方法要在类实例化后才存在。 非静态方法 在面对其他函数中调用,需要对函数进行实例化
值传递和引用传递:
值传递:
int a= 1
public static int test(int a){
a = 10;
}
test(a)
以上程序执行之后,a的值还是1,因为只是传递了一个形参给test函数,a本事并没有被改变。
引用传递:
class person{
String name;
}
public static void change(person a){
a.name = “111111‘
}
person b = new person();
change(b)
这种情况b内部的值会被改变,这是因为b作为一个类,change改变的是对象的属性。_
this的用法:
public class Student{
String name;
int age;
public void st(){
this.name = '张三'
}
}
这个this类似于python中的self
构造器
这个类似于python中的__init__(),方法名字必须和类名相同。一般都是为了给类进行初始化定义的。
对于java,定义的方式如下:
public class person{
String name;
public person{
this.name = '.....'
}
}
封装
属性私有,get/set
追求:高内聚,低耦合
private String name; 这样,实例后就不可以调用了
继承
public class Student extends Person{
//学生继承人的属性,属于派生类
public Student(){
//构造函数的使用
super();//先调用父类的方法
//然后再调用子类的构造函数
}
this.funtion()//调用子类的方法
super.funtion()//调用父类的方法、
}
今天的学习任务就到,还要写python代码,二者很像



