Java方法查漏补缺
一:
package com.oop;
import java.io.IOException;
import java.util.Scanner;
// Demo01 类
public class Demo01 {
// main 方法
public static void main(String[] args) {
int sum = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("please enter two counts choose the bigger count:");
int a = scanner.nextInt();
int b = scanner.nextInt();
sum = max(a,b);
if (sum == 0){
System.out.println(b);
}else {
System.out.println(a);
}
scanner.close();
}
/*
修饰符 返回值类型 方法名字(...){
方法体
return 返回值;
}
*/
public static String sayHello(){
return "Hello World"; // return代表方法结束 返回一个结果
}
public static int max(int a,int b){
return a > b ? 1 : 0; // 三元运算符
}
}
二:
package com.oop;
public class Demo02 {
// 静态方法 static
// 非静态方法
public static void main(String[] args) {
Student.speak(); // 静态方法调用
// 非静态方法调用:(常用)
// 实例化这个类 new
// 对象类型 对象名字 = 对象值;
Student student = new Student();
student.say();
}
// static 和类一起加载
public void a(){
b();
}
// 类实例化 后才存在
public void b(){}
}
三:
package com.oop;
public class Demo04 { // 值传递与引用传递练习
public static void main(String[] args) {
}
// 返回值为空
public static void change(int a){
a = 10;
}
}
四:
package com.oop;
/***
* ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
* ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
* ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
* ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
* ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
* ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
* ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
* ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
* ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
* ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
* ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
* 单身狗就这样默默地看着你,一句话也不说。
*/
public class Demo05 { // 引用传递
public static void main(String[] args) {
Person person = new Person();
System.out.println(person);
System.out.println(person.name);
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
// person是一个对象 指向的是------>Person person = new Person();这是一个具体的人 可以改变属性
person.name = "LittleWu";
}
}
// 定义了一个Person类 有一个属性:name
class Person{
String name;
}