1.如何定義類的方法
步驟一:定義方法名以及返回值類型
步驟二:編寫方法體
2.方法的返回值
兩種情況
如果方法具有返回值,方法中必須使用關鍵字return返回該值,返回值類型為該返回值的類型
如果方法沒有返回值,返回值類型為void
舉例:
public class AutoLion {
//定義變量
public String color;
//跑 沒有返回值 void
public void run(){
System.out.println("lion running...");
}
//有返回值
//return 終止程序 跳出方法
public String bark(){
String voice="獅子吼";
return voice;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
//沒有返回值
al.run();
// 帶返回值的方法如何處理?
// 帶返回值的方法:必須接收返回值
String voice=al.bark();
System.out.println(voice); 會處理結果
}
}
3.方法調用
舉例
public class AutoLion {
//定義變量 屬性file的值可以做返回
public String color;
//返回獅子的顏色
public String getColor(){
return color;
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
//返回顏色測試
al.color="red";
String color=al.getColor();
System.out.println(color);
}
}
方法的嵌套(調用)
public class AutoLion {
//定義變量
public String color;
//返回獅子的顏色
public String getColor(){
return color;
}
//返回獅子的屬性
//在一個方法中調用另外一個方法
public String info(){
return "這是一只"+getColor()+"顏色的獅子";
}
}
public class AutoLionTest {
public static void main(String[] args) {
AutoLion al=new AutoLion();
al.color="red";
String color=al.info();
System.out.println(color);
}
}
5.方法調用小結
1.Student類的方法a( )調用Student類的方法b( ),直接調用
public void a( ) {
b( ); //調用b( )
}
2.Student類的方法a( )調用Teacher類的方法b( ),先創建類對象,然后使用“.”調用
public void a( ) {
Teacher t = new Teacher( );
t.b( ); //調用Teacher類的b()
}
舉例:學生方法調用老師方法
6.常見錯誤
方法的返回值類型為void,方法中不能有return 返回值!
方法不能返回多個值!
多個方法不能相互嵌套定義!
不能在方法外部直接寫程序邏輯代碼!
練習:編寫電池類
編寫電池類(Cell):具有品牌屬性brand,可以續電getPrower()
編寫測試類(TestCell)
public class Cell {
public String brand;
public void getPrower(){
System.out.println("充電中...");
}
}
public class CellTest {
public static void main(String[] args) {
Cell nanfu=new Cell();
nanfu.brand="南孚";
String brand=nanfu.brand;
System.out.println(brand);
nanfu.getPrower();
}
}
練習:計算平均分和總成績
public class ScoreCalc {
public double java;
public double python;
public double go;
//計算總成績
public double getSum(){
double sum=java+python+go;
return sum;
}
//計算平均成績
public double avg(){
double avg=getSum()/3;
return avg;
}
}
import java.util.Scanner;
public class ScoreCalcTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
ScoreCalc sc=new ScoreCalc();
System.out.println("請輸入JAVA成績");
sc.java=input.nextDouble();
System.out.println("請輸入python成績");
sc.python=input.nextDouble();
System.out.println("請輸入go成績");
sc.go=input.nextDouble();
//計算總分
double sum=sc.getSum();
double avg=sc.avg();
System.out.println("總分是"+sum+"平均分"+avg);
}
}
7.成員變量和局部變量
變量聲明的位置決定變量作用域
變量作用域確定可在程序中按變量名訪問該變量的區域
8.成員變量和局部變量的區別
作用域不同
1-局部變量的作用域僅限於定義它的方法
2-成員變量的作用域在整個類內部都是可見的
初始值不同
1-Java會給成員變量一個初始值
2-Java不會給局部變量賦予初始值
9.成員變量和局部變量常見錯誤
局部變量avg的作用域僅限於calcAvg()方法
public class Test {
int score1 = 88;
int score2 = 98;
public void calcAvg() {
int avg = (score1 + score2)/2;
}
public void showAvg(){
System.out.println("平均分是: " + avg);
}
}
練習:
編寫手機類(Phone)
可以下載音樂,可以播放這些音樂,可以進行充電
重用電池類方法(Cell)
編寫測試類(TestPhone)
public class Phone {
//下載音樂
public String download(){
String music="喵喵喵";
return music;
}
//播放音樂
public void play(){
String music=download();
System.out.println("播放"+music);
}
//充電
public void charge(){
Cell cell=new Cell();
cell.getPrower();
}
}
public class PhoneTest {
public static void main(String[] args) {
Phone a=new Phone();
a.download();
a.play();
a.charge();
}
}
心得:本類方法與不同類方法的嵌套
練習:實現菜單的級聯效果
import java.util.Scanner;
public class Menu {
//登陸菜單
public void showLoginMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue){
System.out.println("歡迎使用我行我素購物管理系統");
System.out.println("1.登陸系統");
System.out.println("2.退出");
System.out.println("請選擇....");
int choice=input.nextInt() ;
if(choice==1){
showMainMenu();
break;
}else if(choice==2){
break;
}else{
System.out.println("對不起,輸入有誤,請重新輸入");
}
}
}
//顯示主菜單
public void showMainMenu(){
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println("我行我素購物管理系統主菜單");
System.out.println("--------------------------");
System.out.println("登陸系統");
System.out.println("1.客戶信息管理2.真情回饋");
System.out.println("請選擇....");
int choice=input.nextInt();
if (choice==1) {
break;
}else if(choice==2){
showSendGMenu();
break;
}else if(choice==0){
showLoginMenu();
}else{
System.out.println("對不起,輸入有誤,請重新輸入");
}
}
}
//顯示客戶信息
public void showSendGMenu(){
Scanner input=new Scanner(System.in);
System.out.println("我行我素購物管理系統>>真情回饋");
System.out.println("------------------------------");
System.out.println("1-幸運大放送");
System.out.println("2-幸運抽獎");
System.out.println("3-生日問候");
Boolean isTrue=true;
while(isTrue){
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println("執行幸運大放送");
break;
case 2:
System.out.println("執行幸運抽獎");
break;
case 3:
System.out.println("執行生日問候");
break;
default:
System.out.println("對不起,請重新輸入");
break;
}
}
}
}
測試類
public class MenuTest {
public static void main(String[] args) {
Menu m=new Menu();
m.showLoginMenu();
}
}
練習:實現系統入口程序
需求說明
編寫類StartSMS,
實現輸入用戶名和密碼,
符合條件的進入系統
package demo;
// 人員類
public class Manager {
public String username="afu";
public String password="123";
public void show() {
System.out.println("名字:"+username+"密碼:"+password);
}
}
package demo;
import java.util.Scanner;
public class Menu {
//顯示登陸菜單
public void showLoginMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while(isTrue) {
System.out.println("歡迎登陸我行我素購物系統");
System.out.println("1-登陸");
System.out.println("2-退出");
System.out.println("請選擇");
int choice=input.nextInt();
if(choice==1) {
//
break;
}else if(choice==2) {
break;
}else {
System.out.println("對不起,輸入有誤,請重新輸入");
}
}
}
//顯示主菜單
public void showMainMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println("我行我素購物管理系統");
System.out.println("---------------------------------------------------");
System.out.println("1-客戶信息管理");
System.out.println("2-真情回饋");
int choice=input.nextInt();
if(choice==1) {
break;
}else if(choice==2) {
showCustomerMenu();
break;
}else {
}
}
}
//顯示客戶信息
public void showCustomerMenu() {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
System.out.println("我行我素購物管理系統>>真情回饋");
System.out.println("---------------------------------------------------");
System.out.println("1-幸運大放送");
System.out.println("2-幸運抽獎");
System.out.println("3-生日問候");
int choice=input.nextInt();
switch (choice) {
case 1:
System.out.println("幸運大放送");
isTrue=false;
break;
case 2:
System.out.println("幸運抽獎");
isTrue=false;
break;
case 3:
System.out.println("生日問候");
isTrue=false;
break;
default:
System.out.println("對不起,請重新輸入");
break;
}
}
}
}
package demo;
import java.util.Scanner;
public class StartSMS {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean isTrue=true;
while (isTrue) {
Manager manager=new Manager();
Menu menu=new Menu();
menu.showLoginMenu();
System.out.println("請輸入用戶名");
String username=input.next();
System.out.println("請輸入密碼");
String pwd=input.next();
if(username.equals(manager.username)&&pwd.equals(manager.password)) {
System.out.println("登陸成功");
System.out.println("歡迎"+manager.username+"login");
/* menu.showMainMenu();*/
break;
}else {
System.out.println("對不起,您沒有權限");
}
}
}
}