当前位置: 首页 > news >正文

Java面向对象(二)(类的方法)(自己学习整理的资料)

目录

一.带参方法

带1个参数的方法

带2个参数的方法

带3个参数的方法

数组作为参数

(图书管理系统)

递归方法

二.简易银行存取款功能


一.带参方法

  • 语法

<访问修饰符>返回类型<方法名>(<形式参数列表>){

//方法的主体

}

  • 带1个参数的方法
package lian;public class Dog {//类的成员String name;//成员变量//成员方法public void eat(String food){System.out.println("吃" + food);}
}
package lian;public class dog2 {public static void main(String[] args) {Dog dog = new Dog();dog.eat("骨头");}
}

ps:图截取自视频老师讲解。

  • 带2个参数的方法
package lian;public class Calc1 {public int add(int num1,int num2){int result = num1 + num2;return result;}}
package lian;public class calc2 {public static void main(String[] args) {Calc1 calc = new Calc1();int num1 = 3,num2 = 5;int result = calc.add(num1, num2);System.out.println(result);}
}

ps:图截取自视频老师讲解。

  • 带3个参数的方法
package lian;public class Calc1 {public int calcData(int num1, int num2, char oper) {int result = 0;switch (oper) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':if (num2 != 0) {result = num1 / num2;} else {result = -1;}
break;default:result = -2;//操作符错误break;}return result;}
}
package lian;import java.util.Scanner;public class lian1 {public static void main(String[] args) {Calc1 calc = new Calc1();Scanner input = new Scanner(System.in);System.out.println("请输入第一个数字:");int num1 = input.nextInt();System.out.println("请输入第二个数字:");int num2 = input.nextInt();System.out.println("请输入运算符:");char oper = input.next().charAt(0);int result = calc.calcData(num1, num2, oper);if (result == -1) {//证明现在除数为0System.out.println("除数不能为0!");} else if (result == -2) {System.out.println("运算符错误!");}System.out.println(result);}
}
  • 数组作为参数

练习

  • (一)小明班级里有5位同学参加了数学竞赛,输出比赛的平均成绩和最高成绩。
package lian;public class Calc {//定义一个求平均成绩的方法public double getAvgScore(int[] scores){int sum = 0;for (int i = 0;i < scores.length;i++){sum += scores[i];}//平均分=总分/总人数double avgScore = sum*1.0/scores.length;return avgScore;}//定义一个求最高分的方法
public int getMaxScore(int[] scores){//求最高分,核心思想,默认第一项是最高分int max = scores[0];for (int i = 1;i < scores.length;i++){if (scores[i] > max){max = scores[i];}}return max;
}
}
package lian;import java.util.Scanner;public class lian1 {public static void main(String[] args) {//实例化Calcint[] scores = new int[5];Calc calc = new Calc();Scanner input = new Scanner(System.in);System.out.println("请输入5名学员的成绩:");//数组赋值的操作for (int i = 0;i < scores.length;i++){scores[i] = input.nextInt();}double avgScore = calc.getAvgScore(scores);int maxScore = calc.getMaxScore(scores);System.out.println("平均分为:"+ avgScore);System.out.println("最高分为:"+ maxScore);}
}
  • (二)创建一个Movie类,有电影名称、导演、演员三个属性。写一个打印所有属性信息的方法。实例化两个电影对象。
package lian;/*** 定义一个电影类*/
public class Movile {String name;String director;String[] actors;//写一个可以给演员数组赋值的方法public void initActors(String[] acts){actors = acts;}//介绍影片阵容的一个方法public void show(){System.out.println("电影名称为:" + name);System.out.println("电影导演为:" + director);System.out.println("电影的演员为:");for (int i = 0;i < actors.length;i++){System.out.println(actors[i]);}}
}
package lian;public class dianying {public static void main(String[] args) {Movile movile = new Movile();movile.name="人生";movile.director = "李";String[] actors = new String[5];actors[0]="张三";actors[1]="李四";actors[2]="王五";actors[3]="赵二";actors[4]="刘一";movile.initActors(actors);movile.show();System.out.println("==================");Movile movile2 = new Movile();movile2.name = "中国革命军";movile2.director = "无名";String[] actors2=new String[5];actors[0]="成龙";actors[1]="文章";actors[2]="李小龙";actors[3]="李连杰";actors[4]="吴孟达";movile2.initActors(actors2);movile2.show();}
}
  • (三)制作一个显示所有图书列表和添加图书功能的系统。让用户可以方便的添加和查看图书。
  • (图书管理系统)
package lian;/***定义图书类*/
public class Book {
//记录图书信息String bookname;//图书名称String bookauthor;//作者int price;
}
package lian;/*** 针对图书的管理类*/
public class BookManager {//维护一个数组,数组的类型,图书类型,对象数组Book[] books = new Book[50];//对books进行初始化,给前两项赋值public void initBooks(){//构建一个图书对象Book book = new Book();book.bookname="阿泰勒的角落";book.bookauthor = "李娟";book.price=45;books[0]=book;Book book2 = new Book();book2.bookname="平凡的世界";book2.bookauthor = "路遥";book2.price=55;books[1]=book2;}//图书列表
public void booklist(){System.out.println("这里是图书列表:");System.out.println("书名\t\t作者\t\t价格");for (int i = 0;i < books.length;i++ ) {if (books[i] != null) {System.out.println(books[i].bookname + "\t\t" + books[i].bookauthor + "\t\t" + books[i].price);}}
}//添加图书public boolean addBook(Book book) {boolean flag = false;for (int i = 0; i < books.length; i++) {if (books[i] == null) {//判定对象是否为空,和null相比较books[i] = book;flag = true;break;}}return flag;}
}
package lian;import java.util.Scanner;public class jiemian {public static void menu(){System.out.println("欢迎来到图书管理系统");System.out.println("1、图书列表");System.out.println("2、添加图书");System.out.println("0、退出系统");System.out.println("请输入对应操作,输入数字即可");}public static void main(String[] args) {BookManager manager=new BookManager();manager.initBooks();menu();Scanner input = new Scanner(System.in);int num = input.nextInt();while(num!=0){//规定0是结束程序if (num==1){//图书列表manager.booklist();menu();num = input.nextInt();}else if (num==2) {//添加操作System.out.println("这里是添加图书界面:");Book book = new Book();System.out.println("请输入图书名称:");book.bookname = input.next();System.out.println("请输入图书作者:");book.bookauthor = input.next();System.out.println("请输入图书价格:");book.price = input.nextInt();boolean flag = manager.addBook(book);if (flag == true) {System.out.println("添加成功!");} else {System.out.println("添加失败!");}menu();num = input.nextInt();}else if (num==0){break;}}System.out.println("感谢您使用该系统");}
}

  • 递归方法

方法调用自己,这种调用称为“递归”调用,这样的方法被称为“递归方法”。

一个问题要想用递归的方法来解决,必须要符合两个条件:

  1. 可以把这个问题转化成一个新问题,而新问题的解法和原问题的解法完全相同,只是问题规模变小了;
  2. 必须要有一个明确的递归结束事件(递归边界)。

练习

  • 求阶乘   编程求n阶乘的值,n!=1×2×3×···×(n-1)×n。
package lian;import java.util.Scanner;public class jiecheng {public static int func(int n){if (n==1){return 1;}else {return n * func(n-1);}}public static void main(String[] args) {System.out.println("请输入一个数:");//求n的阶乘Scanner input = new Scanner(System.in);int n =input.nextInt();System.out.println(func(n));}
}

二.简易银行存取款功能

package lian;/*** 定义一个账号类*/
public class Account {double money;//定义账号金额//01、查询余额public double getMoney(){return money;}//02、存款
public void save(double num){money += num;System.out.println("存款成功!");System.out.println("当前余额为:"+money);
}//03、取款public void qu(double num){//判定一下if (money >= num){money -= num;System.out.println("取款成功!");System.out.println("当前余额为:"+money);}else {System.out.println("余额不足!");System.out.println("当前余额为:"+money);}}
}
package lian;import java.security.spec.RSAOtherPrimeInfo;
import java.util.Scanner;public class Back {public static void main(String[] args) {//存取款功能Account account = new Account();Scanner input = new Scanner(System.in);while (true) {System.out.println("1、存款   2、取款    0、退出   请选择您要办理的业务:");int num = input.nextInt();if (num == 1) {//存款System.out.println("这里是存储页面");System.out.println("请输入存款金额:");double money = input.nextDouble();account.save(money);} else if (num == 2) {System.out.println("这里是取款界面");System.out.println("请输入取款金额:");double money = input.nextDouble();account.qu(money);} else if (num == 0) {System.out.println("感谢您选择本银行");break;} else {System.out.println("选择有误,请重新选择!");}}}
}


http://www.mrgr.cn/news/36247.html

相关文章:

  • 2-103 基于matlab的光电信号下血氧饱和度计算
  • nginx部署手册
  • Linux 系统安全工具简介
  • 【机器学习】Flux.jl 生态
  • 静电势能(electrostatic potential energy)和电势(electric potential)
  • Windows (rust) vulkan 画一个三角形: 窗口创建与渲染初始化
  • 如何创建一个包含多个列的表?
  • keil的debug功能
  • Pycharm Python PyAutoGUI包 返回 “ImageNotFoundException“问题
  • Qemu开发ARM篇-6、emmc/SD卡AB分区镜像制作
  • docker搭建clickhouse并初始化用户名密码
  • 算法复杂度
  • 多层时间轮实现延迟消息
  • linux网络编程8
  • 使用Docker和Macvlan驱动程序模拟跨主机跨网段通信
  • 代理有什么用处?
  • 数据结构const char *INSTNAME[]
  • C++——输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。用string方法。
  • 个人文章汇总
  • 类与对象—python