文章目录
返回值类型练习
返回数组
package test01;
public class Manager {
public int[] a() {
int num1 = 10;
int num2 = 20;
int nums[] = {num1,num2};
return nums;
}
}
package test01;
public class Test {
public static void main(String[] args) {
Manager mgr = new Manager();
int ages[] = mgr.a();
for(int i : ages) {
System.out.println(i);
}
}
}
返回类型
//未知
package test01;
public class Test {
public static void main(String[] args) {
Manager mgr = new Manager();
Student s = mgr.a();
System.out.println(s.name);
System.out.println(s.age);
System.out.println(s.sex);
}
}
//未知
package test01;
public class Manager {
public Student a() {
String name = "张三";
int age = 33;
String sex = "男";
Student stu = new Student();
stu.name = name;
stu.age = age;
stu.sex = sex;
return stu;
}
}
//Student类
package test01;
public class Student {
public String name;
public int age;
public String sex;
}
参数列表
参数:多int 返回值:单int
package test02;
public class Calculator {
public int calc(int num1,int num2) {
int result = num1 + num2;
return result;
}
}
package test02;
public class Test {
public static void main(String[] args) {
Calculator c = new Calculator(); //实例化对象
int num1 = 15;
int num2 = 37;
int result = c.calc(num1,num2);
System.out.println(result);
}
}
形参、实参练习
package test03;
public class ZhaZhiJi {
//形参:形式上的参数(数据类型 参数名)
public void zha(String fruit) {
System.out.println(fruit + "汁");
}
}
package test03;
public class Test {
public static void main(String[] args) {
ZhaZhiJi zzj = new ZhaZhiJi();
String fruit = "苹果";
//实参:实际的参数(变量或值)
zzj.zha(fruit);
}
}
参数:arr 返回值:int
package test04;
public class Calculator {
public int getSum(int [] arr) {
int sum = 0;
for(int i : arr) {
sum += i;
}
return sum;
}
}
package test04;
public class Test {
public static void main(String[] args) {
int arr[] = {1,3,5,7,9};
Calculator c = new Calculator();
int result = c.getSum(arr);
System.out.println(result);
}
}
get:调试模式,双击打点,F6下一步,F5进入函数
参数:对象 返回值:多类型
package test05;
public class Student {
public String name;
public int age;
}
package test05;
public class Test {
public static void main(String[] args) {
Manager mgr = new Manager();
Student stu = new Student();
stu.name = "alice";
stu.age = 19;
mgr.show(stu);
}
}
package test05;
public class Manager {
public void show(Student stu) {
System.out.println("大家好,我叫" + stu.name + ",我的年龄是" + stu.age + "岁");
}
}
对象数组
package test06;
public class Student {
public String name;
public int age;
}
package test06;
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.name = "alice";
stu1.age = 19;
Student stu2 = new Student();
stu2.name = "tony";
stu2.age = 20;
Student [] stuArr = new Student[2];
stuArr[0] = stu1;
stuArr[1] = stu2;
Manager mgr = new Manager();
mgr.show(stuArr);
}
}
package test06;
public class Manager {
public void show(Student[] stuArr) {
for(Student stu : stuArr) {
System.out.println(stu.name + "\t");
System.out.println(stu.age + "\n");
}
}
}
方法
重载方法
package test07;
public class Student {
//重载方法:在一个类中,方法名相同,参数项不同(个数、类型、顺序)的方法
//wjj:不完全一样就不同
public void show(String name) {
System.out.println(name);
}
public void show(int age) {
System.out.println(age);
}
public void show(String name,int age) {
System.out.println(name + age);
}
}
package test07;
public class Test {
public static void main(String[] args) {
Student stu = new Student();
stu.show("alice");
stu.show(19);
stu.show("tony",20);
}
}
不构成重载方法
1.相同类型,形参名称不同
2.方法名一样,参数类型一样,只有返回值不一样,不构成重载
构造方法
构造方法也是重载方法
定义:在一个类中,方法名跟类名相同,没有返回类型的方法
实例化就是调用方法,构造方法在实例化对象时,通过【new】关键字调用
如果没有自定义构造方法,系统将默认分配一个【隐式】【无参】的构造方法
使用重载方法写一个无参的:
面向对象
知识点:
封装
面向对象的第一大特征,抽取事物的共同特征,写入到一个类的过程
封装的内容:
属性封装(静态特征)
方法封装(动态特征)
封装的使用场景:
业务类的封装(能应用):如上面的Calculator
数据类(实体类)的封装:如何承载数据(模拟数据库),如上面的Student
package test09;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId() {
this.id = id;
}
public String getName() {
return name;
}
public void setName() {
this.name = name;
}
//有参无参的构造方法
public Student() {
}
public Student(int id,String name) {
this.id = id;
this.name = name;
}
}
package test09;
public class Test {
public static void main(String[] args) {
// Student stu = new Student();
Student stu = new Student(2,"abc");
// stu.setId(777);
int age = stu.getId();
System.out.println(age);
String name = stu.getName();
System.out.println(name);
}
}
Eclipse代码补全:
右键、source
或Alt+Shift+S
Generate Getters and Setters
Alt+A全选:创造所有属性的方法
Generate Constructor using Fields
Alt+D全不选:无参构造
Alt+A全选:有参构造
使用属性构造函数(有参/无参)
继承
待学习
多态
待学习