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();
会处理结果
}
}

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( )

}

2.Student类的方法a( )调用Teacher类的方法b( ),先创建类对象,然后使用“.”调用

public void a( ) {
      Teacher t = new Teacher( );
调用Teacher类的b()
}

举例:学生方法调用老师方法

java啥是无参和有参 java类的无参方法_局部变量

 

 

java啥是无参和有参 java类的无参方法_java啥是无参和有参_02

 

java啥是无参和有参 java类的无参方法_java基础_03

 

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(){
平均分是: " + 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){
欢迎使用我行我素购物管理系统");
登陆系统");
退出");
请选择....");
       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("--------------------------");
登陆系统");
客户信息管理2.真情回馈");
请选择....");
       int choice=input.nextInt();
       if (choice==1) {
    break;
     }else if(choice==2){
     showSendGMenu();
    break;
    }else if(choice==0){
     showLoginMenu();
      }else{
对不起,输入有误,请重新输入");
      }
     }    
}
//显示客户信息
    public void showSendGMenu(){
     Scanner input=new Scanner(System.in);
我行我素购物管理系统>>真情回馈");
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:
幸运大放送");
  isTrue=false;
break;
case 2:
幸运抽奖");
  isTrue=false;
break;
case 3:
生日问候");
  isTrue=false;
break;
default:
对不起,请重新输入");
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();
请输入用户名");
    String username=input.next();
请输入密码");
    String pwd=input.next();
    if(username.equals(manager.username)&&pwd.equals(manager.password)) {
登陆成功");
欢迎"+manager.username+"login");
    /* menu.showMainMenu();*/
     break;
    }else {
对不起,您没有权限");
    }
 }
}
}