首页
关于
友链
Search
1
java project 客户管理信息软件
129 阅读
2
java学习笔记10
114 阅读
3
java学习笔记12
109 阅读
4
java学习笔记1
103 阅读
5
java学习笔记11
98 阅读
默认分类
程序设计
java
vue前端
java学习笔记
java练习
java工程
登录
Search
标签搜索
java
java基础
学习笔记
练习
类
java进阶
数组
循环
vue
工程
helloworld
注释
关键字
变量
基本数据类型
选择
String
进制
运算符
Scanner
bandao
累计撰写
19
篇文章
累计收到
1
条评论
首页
栏目
默认分类
程序设计
java
vue前端
java学习笔记
java练习
java工程
页面
关于
友链
搜索到
19
篇与
的结果
2022-07-22
2022_07_21施工进度
2022_07_21施工进度{music id="1463391795" color="#1989fa" /}点击 http://test.bandao.ltd 再点击歌单再点击音乐可以播放歌曲,下面的播放暂停按钮可正常使用主要难点:本地要代理访问网易云的api,线上则不需要自己代理(这里花了两个小时来更正修复bug:刷新导致404// vue.config.js devServer: { proxy: { '/api': { target: 'https://music.163.com',//这里填入你要请求的接口的前缀 ws: true,//代理websocked changeOrigin: true,//虚拟的站点需要更管origin secure: true, //是否https接口 pathRewrite: { '^/api': ''//重写路径 } } } }// footerMusic.vue <!-- 本地使用这个,这里用了代理 --> <!-- <audio ref="myAudio" :src="`/api/song/media/outer/url?id=${playlist[playlistIndex].id}.mp3`"></audio> --> <!-- 线上使用这个 --> <audio ref="myAudio" :src="`https://music.163.com/song/media/outer/url?id=${playlist[playlistIndex].id}.mp3`"></audio> 技术点vuex// store/index.js mutations: { updateIsBtnShow(state, value) { state.isBtnShow = value }, updatePlaylist(state, value) { state.playlist = value }, updatePlaylistIndex(state, value) { state.playlistIndex = value }, },// footerMusic.vue import { mapMutations, mapState } from 'vuex' computed: { ...mapState(['playlist', 'playlistIndex', 'isBtnShow']), }, methods: { ..mapMutations(['updateIsBtnShow']) },
2022年07月22日
65 阅读
0 评论
0 点赞
2022-07-20
2022_07_19施工进度
2022-07-20 03:29:47 星期三{music id="1958557200" color="#1989fa" /}{collapse}{collapse-item label="Daily部分" close}音乐没开自动播放好困啊,明天还要练车,绝了此页面随时消失嗷,说不定那天我又把服务器重启了,ngnix又开不了了,这个页面就g了图片放在我另外一个网站上了也是随时会g掉明天还要处理一堆拍的照片{/collapse-item}{/collapse}2022_07_19施工进度点击 http://test.bandao.ltd 再点击歌单网址是 http://test.bandao.ltd/itemMusic?id=5349291073 (好像直接点击进不去耶,显示404我也没搞懂)今天做的事情:上面的歌单信息展示部分写了200多行,大部分都是样式,写得我自己都要看不懂了已知bug :直接点击链接或者再次刷新会404,但是本地运行没有这个情况歌单描述部分的箭头需要更改样式pc端显示大概率异常需要努力的地方:还需要了解一下flex布局加快学习的速度啊(原神活动主要都肝完了要好好学习了)下面是代码部分<template> <img :src="playlist.coverImgUrl" alt="" class="bgImg"> <van-row justify="space-between" align="center" class="itemMusicTop"> <van-col span="8" class="itemLeft"> <van-icon name="arrow-left" @click="$router.go(-1)" size="25" /> <span>歌单</span> </van-col> <van-col span="8" class="itemRight"> <van-icon name="search" class="search" size="25" /> <van-icon name="wap-nav" class="wap-nav" size="25" /> </van-col> </van-row> <van-row justify="space-between" align="center" class="itemMusicCenter"> <van-col span="10" class="imgBox"> <div class="imgBoxAndplayCount"> <span class="playCount"> <svg class="icon" aria-hidden="true"> <use xlink:href='#icon-bofang1'></use> </svg> {{ changeCount(playlist.playCount) }} </span> <img :src="playlist.coverImgUrl" alt="" class="coverImg"> </div> </van-col> <van-col span="14"> <van-row justify="center" class="listInfo"> <van-col span="30" class="title">{{ playlist.name }}</van-col> <van-col span="30" class="creator"> <img :src="playlist.creator.avatarUrl" alt=""> <span> {{ playlist.creator.nickname }} <van-icon name="arrow" size="15" /> </span> </van-col> <van-col span="30" class="description"> <div class="text"> {{ playlist.description }} </div> <van-icon name="arrow" size="15" /> </van-col> </van-row> </van-col> </van-row> <van-row justify="space-between" align="center" class="functionButtons"> <van-col span="6"> <van-icon name="chat-o" size="30" /> <span>{{ playlist.commentCount }}</span> </van-col> <van-col span="6"> <van-icon name="share-o" size="30" /> <span>{{ playlist.shareCount }}</span> </van-col> <van-col span="6"> <van-icon name="down" size="30" /> <span>下载</span> </van-col> <van-col span="6"> <van-icon name="add-o" size="30" /> <span>多选</span> </van-col> </van-row> </template> <script> import { getMusicItemList } from '@/request/api/item' export default { name: "itemMusicTop", props: ['playlist'], setup(props) { // console.log(props); // 通过props进行传值,判断如果没有,则从sessionStorage获取 if ((props.playlist.creator = "")) { try { console.log("here"); props.playlist.creator = JSON.parse(sessionStorage.getItem('itemDetail')).playlist.creator } catch { console.log("here"); let response = getMusicItemList(playlistId) playlist = response.data.playlist } } function changeCount(num) { if (num >= 100000000) { // toFixed(1) 小数点后一位 return (num / 100000000).toFixed(1) + "亿" } else if (num >= 10000) { return (num / 10000).toFixed(1) + "万" } } return { changeCount } } } </script> <style lang="less" scoped> .itemMusicTop { height: 50px; padding: 10px; .itemLeft, .itemRight { .van-icon { color: #fff; } } .itemLeft { span { position: relative; font-size: 25px; margin-left: 30px; top: -2.5px; color: #fff; text-align: center; } } .itemRight { position: absolute; right: 10px; .search { margin-right: 40px; } } } .bgImg { position: fixed; width: 100%; height: 350px; z-index: -1; filter: blur(40px); } .itemMusicCenter { margin-top: -15px; padding: 10px; height: 180px; .imgBox { padding: 5px; width: 150px; text-align: center; .imgBoxAndplayCount { position: relative; height: 130px; width: 130px; .coverImg { border-radius: 0.4rem; height: 130px; width: 130px; } .playCount { position: absolute; right: 0.1rem; top: 0.1rem; font-size: 0.25rem; color: #ccc; display: flex; align-items: center; background-color: rgba(0, 0, 0, 0.2); margin: 4px; padding: 3px; border-radius: 0.4rem; .icon { width: 15px; fill: #ccc; } } } } .listInfo { padding: 5px; flex-flow: column; width: 100%; height: 150px; .title { font-weight: 900; color: #fff; font-size: .39rem; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } .creator { padding: 10px 5px; color: #ccc; img { position: relative; border-radius: 0.6rem; height: 30px; } span { position: absolute; padding-left: 10px; width: 160px; font-size: .32rem; line-height: 30px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; } .van-icon { position: absolute; bottom: 7.5px; padding-left: 15px; } } .description { position: relative; color: #ccc; // width: 80%; height: 80px; .text { position: absolute; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; overflow: hidden; width: 85%; } .van-icon { position: absolute; right: 13px; top: 27px; } } } } .functionButtons { color: #fff; height: 1.5rem; padding: 5px; margin-bottom: 10px; .van-col { display: flex; flex-direction: column; align-items: center; } } </style>
2022年07月20日
75 阅读
0 评论
0 点赞
2022-03-10
java学习笔记12
面向对象的特征之二:继承性 why?一、继承性的好处:减少了代码的冗余,提高了代码的复用性便于功能的扩展为之后多态性的使用,提供了前提二、继承性的格式:class A extends B{}A:子类、派生类、subclassB:父类、超类、基类、superclass2.1体现:一旦子类A继承父类B以后,子类A中就获取了父类B中声明的所有的属性和方法。特别的,父类中声明为private的属性或方法,子类继承父类以后,仍然认为获取了父类中私有的结构。只有因为封装性的影响,使得子类不能直接调用父类的结构而已。2.2 子类继承父类以后,还可以声明自己特有的属性或方法:实现功能的拓展。子类和父类的关系,不同于子集和集合的关系。extends:延展、扩展三、Java中关于继承性的规定:一个类可以被多个子类继承。Java中类的单继承性:一个类只能有一个父类子父类是相对的概念。子类直接继承的父类,称为:直接父类。间接继承的父类称为:间接父类子类继承父类以后,就获取了直接父类以及所有间接父类中声明的属性和方法四、 Object类如果我们没有显式的声明一个类的父类的话,则此类继承于java.lang.Object类所有的java类(除java.lang.Object类之外)都直接或间接的继承于java.lang.Object类意味着,所有的java类具有java.lang.Object类声明的功能。Creature.javapackage com.atguigu.java; /** * @program: project13 * @description: Creature类 * @author: kkx * @create: 2022-03-10 16:31 **/ public class Creature { public void breath(){ System.out.println("呼吸"); } }Person.javapackage com.atguigu.java; /** * @program: project13 * @description: Person类 * @author: kkx * @create: 2022-03-10 15:50 **/ public class Person extends Creature{ String name; private int age; public Person(){ } public Person(String name, int age){ this.name = name; this.age = age; } public void eat(){ System.out.println("吃饭"); } public void sleep(){ System.out.println("睡觉"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }Student.javapackage com.atguigu.java; /** * @program: project13 * @description: Student类,继承 * @author: kkx * @create: 2022-01-05 10:22 **/ public class Student extends Person{ // String name; // int age; String major; public Student(){ } public Student(String name, int age, String major) { this.name = name; // this.age = age; setAge(age); this.major = major; } // public void sleep(){ // System.out.println("睡觉"); // } public void study(){ System.out.println("studying"); } public void show(){ System.out.println("name:" + name + ", age:" + getAge()); } }ExtendsTest.javapackage com.atguigu.java; /** * @program: project13 * @description: 继承测试 * @author: kkx * @create: 2022-01-05 10:24 **/ public class ExtendsTest { public static void main(String[] args) { Person p1 = new Person(); // p1.age = 1; p1.eat(); System.out.println(); Student s1 = new Student(); s1.eat(); s1.sleep(); s1.name = "Tom"; s1.setAge(10); s1.show(); s1.breath(); Creature c = new Creature(); System.out.println(c.toString()); } }
2022年03月10日
109 阅读
0 评论
0 点赞
2022-01-05
java project 客户管理信息软件
客户管理信息软件{music id="1886456269" color="#1989fa" autoplay="autoplay"/}参考视频(p248-p259){bilibili bvid="1Kb411W75N" page="248"/}CustomerView 主模块,菜单显示package com.atguigu.p2.ui; import com.atguigu.p2.bean.Customer; import com.atguigu.p2.service.CustomerList; import com.atguigu.p2.util.CMUtility; /** * @program: project12 * @description: 主模块,菜单显示,处理用户操作 * @author: kkx * @create: 2021-12-28 17:12 **/ public class CustomerView { private CustomerList customerList = new CustomerList(10); public CustomerView(){ //默认有一个 Customer customer = new Customer("王涛", '男', 23, "13212341234", "wt@gmail.com"); customerList.addCustomer(customer); } /** * @Description: 显示《客户信息管理软件》界面的方法 * @Author: kkx * @Date: 2022/1/4 * @Param: [] * @return: void */ public void enterMainMenu(){ boolean isFlag = true; while (isFlag){ System.out.println("\n-----------------客户信息管理软件-----------------\n"); System.out.println(" 1 添 加 客 户"); System.out.println(" 2 修 改 客 户"); System.out.println(" 3 删 除 客 户"); System.out.println(" 4 客 户 列 表"); System.out.println(" 5 退 出\n"); System.out.print(" 请选择(1-5):"); char menu = CMUtility.readMenuSelection(); switch (menu){ case '1': addNewCustomer(); break; case '2': modifyCustomer(); break; case '3': deleteCustomer(); break; case '4': listAllCustomers(); break; case '5': System.out.print ("确认是否退出(Y/N):"); char isExit = CMUtility.readConfirmSelection(); if (isExit == 'Y') { isFlag = false; } break; } } } /** * @Description: 添加客户的操作 * @Author: kkx * @Date: 2022/1/4 * @Param: [] * @return: void */ private void addNewCustomer(){ System.out.println("---------------------添加客户---------------------"); System.out.print("姓名:"); String name = CMUtility.readString(10); System.out.print("性别:"); char gender = CMUtility.readChar(); System.out.print("年龄:"); int age = CMUtility.readInt(); System.out.print("电话:"); String phone = CMUtility.readString(13); System.out.print("邮箱:"); String email = CMUtility.readString(30); //包装 Customer customer = new Customer(name, gender, age, phone, email); boolean isSuccess = customerList.addCustomer(customer); if (isSuccess){ System.out.println("---------------------添加完成---------------------"); }else { System.out.println("-------------------客户目录已满,添加失败---------------"); } } /** * @Description: 修改客户的操作 * @Author: kkx * @Date: 2022/1/5 * @Param: [] * @return: void */ private void modifyCustomer(){ System.out.println("---------------------修改客户---------------------"); //这两个放外面是因为后面在循环外要调用 int number; Customer cust; for (;;){ System.out.print("请选择待修改客户编号(-1退出):"); number = CMUtility.readInt(); if (number == -1){ return; } cust = customerList.getCustomer(number - 1); if (cust == null){ System.out.println("无法找到指定客户!"); }else { break; } } //修改客户信息 System.out.print("姓名(" + cust.getName() + "):"); String name = CMUtility.readString(10, cust.getName()); System.out.print("性别(" + cust.getGender() + "):"); char gender = CMUtility.readChar(cust.getGender()); System.out.print("年龄(" + cust.getAge() + "):"); int age = CMUtility.readInt(cust.getAge()); System.out.print("电话(" + cust.getPhone() + "):"); String phone = CMUtility.readString(13, cust.getPhone()); System.out.print("邮箱(" + cust.getEmail() + "):"); String email = CMUtility.readString(30, cust.getEmail()); Customer newCust = new Customer(name, gender, age, phone, email); boolean isReplaced = customerList.replaceCustomer(number - 1, newCust); if (isReplaced){ System.out.println("---------------------修改完成---------------------"); }else { //没有机会执行 System.out.println("---------------------修改失败---------------------"); } } /** * @Description: 删除客户的操作 * @Author: kkx * @Date: 2022/1/5 * @Param: [] * @return: void */ private void deleteCustomer(){ System.out.println("---------------------删除客户---------------------"); int number; for (;;){ System.out.print("请选择待删除客户编号(-1退出):"); number = CMUtility.readInt(); if (number == -1){ return; } Customer customer = customerList.getCustomer(number -1); if (customer == null){ System.out.println("无法找到指定客户!"); }else { break; } } //找到了指定的客户,开始修改 System.out.print("确认是否删除(Y/N):"); char isDelete = CMUtility.readConfirmSelection(); if (isDelete == 'Y'){ boolean deleteSuccess = customerList.deleteCustomer(number - 1); if (deleteSuccess){ System.out.println("---------------------删除完成---------------------"); }else { //没有机会执行 System.out.println("---------------------删除失败---------------------"); } }else { return; } } /** * @Description: 显示客户列表的操作 * @Author: kkx * @Date: 2022/1/4 * @Param: [] * @return: void */ private void listAllCustomers(){ System.out.println("---------------------------客户列表---------------------------\n"); int total = customerList.getTotal(); if (total == 0){ System.out.println("没有客户记录!"); }else { System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱"); Customer[] custs = customerList.getAllCustomers(); for (int i = 0; i < custs.length; i++) { Customer cust = custs[i]; System.out.println((i + 1) + "\t" + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + "\t" + cust.getAge() + "\t" + "\t" + cust.getPhone() + "\t" + cust.getEmail()); } } System.out.println("-------------------------客户列表完成-------------------------"); } public static void main(String[] args){ CustomerView view = new CustomerView(); view.enterMainMenu(); } }Customer管理package com.atguigu.p2.service; import com.atguigu.p2.bean.Customer; /** * @program: project12 * @description: Customer管理 * @author: kkx * @create: 2021-12-28 17:11 **/ public class CustomerList { private Customer[] customers; private int total; //记录存储的客户的个数 /** * @Description: 构造器,用来初始化customers数组 * @Author: kkx * @Date: 2021/12/28 * @Param: [totalCustomer] 指定customers数组的最大空间 * @return: */ public CustomerList(int totalCustomer){ customers = new Customer[totalCustomer]; } /** * @Description: 添加指定的客户到数组中 * @Author: kkx * @Date: 2021/12/28 * @Param: [customer] * @return: boolean 是否添加成功 */ public boolean addCustomer(Customer customer){ //小于最大则可以添加 if (total < customers.length){ customers[total] = customer; total++; return true; } return false; } /** * @Description: 修改指定索引位置上的客户信息 * @Author: kkx * @Date: 2021/12/28 * @Param: [index, cust] * @return: boolean 是否修改成功 */ public boolean replaceCustomer(int index, Customer cust){ if (index < 0 || index >= total){ return false; } customers[index] = cust; return true; } /** * @Description: 删除指定索引位置上的客户信息 * @Author: kkx * @Date: 2022/1/4 * @Param: [index] * @return: boolean 是否删除成功 */ public boolean deleteCustomer(int index){ if (index < 0 || index >= total){ return false; } for (int i = index; i < total - 1; i++) { customers[i] = customers[i + 1]; } //这里先把最后那个指向null,total再-- customers[--total] = null; return true; } /** * @Description: 获取所有客户的客户信息 * @Author: kkx * @Date: 2022/1/4 * @Param: [] * @return: com.atguigu.p2.bean.Customer[] */ public Customer[] getAllCustomers(){ Customer[] custs = new Customer[total]; for (int i = 0; i < total; i++) { custs[i] = customers[i]; } return custs; } /** * @Description: 返回指定索引位置上的Customer * @Author: kkx * @Date: 2022/1/4 * @Param: [index] * @return: 如果找到元素,则返回,如果没有找到,返回null */ public Customer getCustomer(int index){ if (index < 0 || index >= total){ return null; } return customers[index]; } /** * @Description: 获取存储的客户的数量 * @Author: kkx * @Date: 2022/1/4 * @Param: [] * @return: int */ public int getTotal(){ return total; } }Customer类package com.atguigu.p2.bean; /** * @program: project12 * @description: Customer类 * @author: kkx * @create: 2021-12-28 17:08 **/ public class Customer { private String name; // 姓名 private char gender; // 性别 private int age; // 年龄 private String phone; // 电话 private String email; // 邮箱 public Customer() { } public Customer(String name, char gender, int age, String phone, String email) { this.name = name; this.gender = gender; this.age = age; this.phone = phone; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }CMUtility工具类package com.atguigu.p2.util; /** * @program: project12 * @description: CMUtility工具类 * @author: kkx * @create: 2021-12-28 17:14 **/ import java.util.*; /** CMUtility工具类: 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。 */ public class CMUtility { private static Scanner scanner = new Scanner(System.in); /** 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。 */ public static char readMenuSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false); c = str.charAt(0); if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') { System.out.print("选择错误,请重新输入:"); } else break; } return c; } /** 从键盘读取一个字符,并将其作为方法的返回值。 */ public static char readChar() { String str = readKeyBoard(1, false); return str.charAt(0); } /** 从键盘读取一个字符,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static char readChar(char defaultValue) { String str = readKeyBoard(1, true); return (str.length() == 0) ? defaultValue : str.charAt(0); } /** 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 */ public static int readInt() { int n; for (; ; ) { String str = readKeyBoard(2, false); try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:"); } } return n; } /** 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static int readInt(int defaultValue) { int n; for (; ; ) { String str = readKeyBoard(2, true); if (str.equals("")) { return defaultValue; } try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:"); } } return n; } /** 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 */ public static String readString(int limit) { return readKeyBoard(limit, false); } /** 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static String readString(int limit, String defaultValue) { String str = readKeyBoard(limit, true); return str.equals("")? defaultValue : str; } /** 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。 */ public static char readConfirmSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false).toUpperCase(); c = str.charAt(0); if (c == 'Y' || c == 'N') { break; } else { System.out.print("选择错误,请重新输入:"); } } return c; } private static String readKeyBoard(int limit, boolean blankReturn) { String line = ""; while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.length() == 0) { if (blankReturn) return line; else continue; } if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:"); continue; } break; } return line; } }
2022年01月05日
129 阅读
0 评论
0 点赞
2021-12-19
java学习笔记11
{music id="1901756112" color="#1989fa" /}封装性面向对象的特征一:封装与隐藏一.问题的引入:当创建一个类的对象后,我们可以通过"对象.属性"的方式,对对象的属性赋值。这里,赋值操作要受到属性的数据类型和存储范围的制约。除此之外,没有其他制约条件。但是,通常我们需要给属性赋值加入额外的限制条件。这个条件就不能在属性声明时体现,我们只能通过方法进行限制条件的添加。(比如:setLegs())同时,我们需要避免用户再使用"对象.属性"的方式对属性进行赋值。则需要将属性声明为私有的(private).此时,针对于属性就体现了封装性。二.封装性的体现:我们将类的属性xxx私有化(private),同时,提供公共的(public)方法来获取(getXxx)和设置(setXxx)此属性的值拓展:封装性的体现:如上不对外暴露的私有的方法单例模式三.封装性的体现,需要权限修饰符来配合。1.Java规定的4种权限(从小到大排列):private、缺省、protected 、public2.4种权限可以用来修饰类及类的内部结构:属性、方法、构造器、内部类3.具体的,4种权限都可以用来修饰类的内部结构:属性、方法、构造器、内部类 修饰类的话,只能使用:缺省、public四. 四种访问权限修饰符修饰符类内部同一个包不同包的子类同一个工程privateYes (缺省)YesYes protectedYesYesYes publicYesYesYesYes五.java代码Order类package com.atguigu.java; /** * @program: project11 * @description: Order类 * @author: kkx * @create: 2021-12-18 23:49 **/ public class Order { private int orderPrivate; int orderDefault; public int orderPublic; private void methodPrivate(){ System.out.println("methodPrivate"); } void methodDefault(){ System.out.println("methodDefault"); } public void methodPublic(){ System.out.println("methodPublic"); } }同一个包下面调用类package com.atguigu.java; /** * @program: project11 * @description: Oeder类测试 * @author: kkx * @create: 2021-12-18 23:52 **/ public class OrderTest { public static void main(String[] args) { Order order = new Order(); order.orderDefault = 1; order.orderPublic = 2; // a.legs = 4;//The field Animal.legs is not visible order.methodDefault(); order.methodPublic(); } }不同包下面调用类package com.atguigu.java1; import com.atguigu.java.Order; /** * @program: project11 * @description: Oeder类测试 * @author: kkx * @create: 2021-12-19 00:13 **/ public class OrderTest { public static void main(String[] args) { Order order = new Order(); //出了Order类所属的包之后,私有的结构、缺省声明的结构就不可以调用了 //order.orderDefault = 1; order.orderPublic = 2; //order.methodDefault(); order.methodPublic(); } }六.封装性总结:Java提供了4种权限修饰符来修饰类及类的内部结构,体现类及类的内部结构在被调用时的可见性的大小。类的结构之三:构造器(或构造方法、constructor)的使用construct:建设、建造。 construction:CCB constructor:建设者一、构造器的作用:创建对象初始化对象的信息二.Tip:如果定义构造器的话,则系统默认提供一个空参的构造器。定义构造器的格式:权限修饰符 类名(形参列表){}。一个类中定义的多个构造器,彼此可以构成重载。一旦我们定义了类的构造器之后,系统就不再提供默认的空参构造器。一个类中,至少会有一个构造器。三.java代码Person类package com.atguigu.exer; /** * @program: project11 * @description: Person类 * @author: kkx * @create: 2021-12-19 00:24 **/ public class Person { private int age; private String name; public Person(){ age = 18; } public Person(String n, int a){ name = n; age = a; } public void setAge(int a){ if (a < 0 || a > 130){ throw new RuntimeException("传入的数据非法!"); //报红 //也可以先sout然后return } age = a; } public int getAge() { return age; } public void setName(String n){ name = n; } public String getName() { return name; } }Person类测试package com.atguigu.exer; /** * @program: project11 * @description: Person类的使用,类权限课后题 * @author: kkx * @create: 2021-12-19 00:25 **/ public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.setAge(12); System.out.println(p1.getName() + "\t" + p1.getAge()); Person p2 = new Person("Tom", 21); System.out.println(p2.getName() + "\t" + p2.getAge()); } }属性赋值的先后顺序默认初始化显式初始化构造器中初始化通过"对象.方法" 或 "对象.属性"的方式,赋值以上操作的先后顺序:① - ② - ③ - ④java代码package com.atguigu.java1; /** * @program: project11 * @description: 属性赋值的先后顺序 * @author: kkx * @create: 2021-12-19 11:33 **/ public class UserTest { public static void main(String[] args) { User u1 = new User(); System.out.println(u1.age); User u2 = new User(2); u2.setAge(3); System.out.println(u2.age); } } class User{ String name; int age = 1; public User(){ } public User(int a){ age = a; } public void setAge(int a){ age = a; } }javaBeanpackage com.atguigu.java1; /** * @program: project11 * @description: JavaBean * @author: kkx * @create: 2021-12-19 11:45 **/ public class Customer { public int id; private String name; public Customer(){ } public void setId(int i){ id = i; } public int getId() { return id; } public void setName(String n){ name = n; } public String getName() { return name; } }this关键字的使用this可以用来修饰、调用:属性、方法、构造器this修饰属性和方法:this为:当前对象 或 当前正在创建的对象在类的方法中,我们可以使用"this.属性"或"this.方法"的方式,调用当前对象属性或方法。 但是,通常情况下,我们都选择省略"this."。特殊情况下,如果方法的形参和类的属性同名时, 我们必须显式的使用"this.变量"的方式,表明此变量是属性,而非形参。在类的构造器同理this调用构造器我们在类的构造器中,可以显式的使用"this(形参列表)"方式,调用本类中指定的其他构造器构造器中不能通过"this(形参列表)"方式调用自己如果一个类中有n个构造器,则最多有 n - 1构造器中使用了"this(形参列表)"规定:"this(形参列表)"必须声明在当前构造器的首行构造器内部,最多只能声明一个"this(形参列表)",用来调用其他的构造器java代码package com.atguigu.java2; /** * @program: project11 * @description: this关键字 * @author: kkx * @create: 2021-12-19 12:00 **/ public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.setAge(1); System.out.println(p1.getAge()); Person p2 = new Person("Tom", 20); System.out.println(p2.getAge()); } } class Person{ private String name; private int age; public Person(){ System.out.println("Person初始化时,需要考虑如下的1,2,3,4"); } public Person(String name) { this(); //Person初始化时,需要考虑如下的1,2,3,4 this.name = name; } public Person(String name, int age) { this(name); //这个必须放首行,每个构造器里面只能放一个this() this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void eat(){ System.out.println("eating"); study(); } public void study(){ System.out.println("studying"); } }boy,gril 测试package com.atguigu.exer2; /** * @program: project11 * @description: Boy类 * @author: kkx * @create: 2021-12-19 12:55 **/ public class Boy { private String name; private int age; public Boy() { } public Boy(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void marry(Girl girl){ System.out.println("我要" + girl.getName()); } public void shout(){ if (this.age >= 22){ System.out.println("yes!"); }else { System.out.println("no~"); } } }package com.atguigu.exer2; /** * @program: project11 * @description: Girl类 * @author: kkx * @create: 2021-12-19 12:55 **/ public class Girl { private String name; private int age; public Girl(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void marry(Boy boy){ System.out.println("我要" + boy.getName()); boy.marry(this); } /** * @Description: 比较两个对象的大小 * @Author: kkx * @Date: 2021/12/19 * @Param: [girl] * @return: 正数:当前对象大; 负数:当前对象小 ; 0:当前对象与形参对象相等 */ public int compare(Girl girl){ if(this.age > girl.age){ return 1; }else if (this.age < girl.age){ return -1; }else { return 0; } } }package com.atguigu.exer2; /** * @program: project11 * @description: Boy,Girl类测试 * @author: kkx * @create: 2021-12-19 12:55 **/ public class BoyGirlTest { public static void main(String[] args) { Boy boy = new Boy("阿强", 21); boy.shout(); Girl girl = new Girl("阿珍", 18); girl.marry(boy); Girl girl1 = new Girl("2233", 11); int compare = girl.compare(girl1); if (compare > 0){ System.out.println(girl.getName() + "大"); }else if (compare < 0){ System.out.println(girl1.getName() + "大"); }else { System.out.println("一样大"); } } }Account测试package com.atguigu.exer3; /** * @program: project11 * @description: Account类 * @author: kkx * @create: 2021-12-19 13:22 **/ public class Account { private int id;//账号 private double balance;//余额 private double annualInterestRate;//年利率 public Account(int id, double balance, double annualInterestRate) { this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public void withdraw (double amount){//取钱 if (balance < amount){ System.out.println("余额不足,取款失败"); return; } balance -= amount; System.out.println("成功取出:" + amount); } public void deposit (double amount){//存钱 if (amount > 0){ balance += amount; System.out.println("成功存入:" + amount); } } }package com.atguigu.exer3; /** * @program: project11 * @description: Customer类 * @author: kkx * @create: 2021-12-19 13:28 **/ public class Customer { private String firstName; private String lastName; private Account account; public Customer(String f, String l) { this.firstName = f; this.lastName = l; } public void setAccount(Account account) { this.account = account; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Account getAccount() { return account; } }package com.atguigu.exer3; /** * @program: project11 * @description: Customer, Account测试 * @author: kkx * @create: 2021-12-19 13:32 **/ public class CustomerTest { public static void main(String[] args) { Customer cust = new Customer("Jane", "Smith"); Account acct =new Account(1000, 2000, 0.0123); cust.setAccount(acct); cust.getAccount().deposit(100); cust.getAccount().withdraw(960); cust.getAccount().withdraw(2000); System.out.println("Customer[" + cust.getLastName() + "," + cust.getLastName() + "]" + "\n" + "id:" + cust.getAccount().getId() + "\n" + "annualInterestRate:" + cust.getAccount().getAnnualInterestRate()*100 + "%" + "\n" + "balance:" +cust.getAccount().getBalance()); } }Bank测试package com.atguigu.exer4; /** * @program: project11 * @description: Account类 * @author: kkx * @create: 2021-12-19 13:43 **/ public class Account { private double balance; public Account(double init_balance) { this.balance = init_balance; } public double getBalance() { return balance; } //存钱操作 public void deposit(double amt){ if(amt > 0){ balance += amt; System.out.println("存钱成功"); } } //取钱操作 public void withdraw(double amt){ if(balance >= amt){ balance -= amt; System.out.println("取钱成功"); }else{ System.out.println("余额不足"); } } }package com.atguigu.exer4; /** * @program: project11 * @description: Bank类 * @author: kkx * @create: 2021-12-19 13:43 **/ public class Bank { private Customer[] customers;// 存放多个客户的数组 private int numberOfCustomers;// 记录客户的个数 public Bank(){ //这里要造数组,否则会空指针 customers = new Customer[10]; } public void addCustomer(String f, String l){ Customer cust = new Customer(f, l); customers[numberOfCustomers] = cust; numberOfCustomers++; //customers[numberOfCustomers++] = cust; } public int getNumberOfCustomers() { return numberOfCustomers; } public Customer getCustomer(int index){ if (index >= 0&& index < numberOfCustomers){ return customers[index]; } return null; } }package com.atguigu.exer4; /** * @program: project11 * @description: Customer类 * @author: kkx * @create: 2021-12-19 13:44 **/ public class Customer { private String firstName; private String lastName; private Account account; public Customer(String f, String l) { this.firstName = f; this.lastName = l; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }package com.atguigu.exer4; /** * @program: project11 * @description: Bank方测试 * @author: kkx * @create: 2021-12-19 13:46 **/ public class BankTest { public static void main(String[] args) { Bank bank = new Bank(); bank.addCustomer("Jane", "Smith"); bank.getCustomer(0).setAccount(new Account(2000)); bank.getCustomer(0).getAccount().withdraw(500); double balance = bank.getCustomer(0).getAccount().getBalance(); System.out.println("客户:" + bank.getCustomer(0).getFirstName() + "的账户余额为:" + balance); System.out.println(); bank.addCustomer("万里", "杨"); System.out.println("银行客户的个数为:" + bank.getNumberOfCustomers()); } }Package的使用为了更好的实现项目中类的管理,提供包的概念使用package声明类或接口所属的包,声明在源文件的首行包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、“见名知意”每"."一次,就代表一层文件目录。JDK中主要的包介绍java.lang----包含一些Java语言的核心类,如String、Math、Integer、 System和Thread,提供常用功能java.net----包含执行与网络相关的操作的类和接口。java.io ----包含能提供多种输入/输出功能的类。java.util----包含一些实用工具类,如定义系统特性、接口的集合框架类、使用与日期日历相关的函数。java.text----包含了一些java格式化相关的类java.sql----包含了java进行JDBC数据库编程的相关类/接口java.awt----包含了构成抽象窗口工具集(abstract window toolkits)的多个类, 这些类被用来构建和管理应用程序的图形用户界面(GUI)。Import的使用在源文件中显式的使用import结构导入指定包下的类、接口声明在包的声明和类的声明之间如果需要导入多个结构,则并列写出即可可以使用"xxx.*"的方式,表示可以导入xxx包下的所有结构在java.lang下的话(java.lang.System 常用),则可以省略import结构如果使用的类或接口是本包下定义的,则可以省略import结构如果在源文件中,使用了不同包下的同名的类,则必须至少有一个类需要以全类名的方式显示。使用"xxx.*"方式表明可以调用xxx包下的所有结构。但是如果使用的是xxx子包下的结构,则仍需要显式导入import static:导入指定类或接口中的静态结构:属性或方法。package com.atguigu.java2; import com.atguigu.exer4.Account; import com.atguigu.exer4.Bank; import com.atguigu.java2.java3.Dog; import java.util.*; //import 类、接口 import static java.lang.System.*; //import static 结构(属性或方法) import static java.lang.Math.*; /** * @program: project11 * @description: PackageImport测试 * @author: kkx * @create: 2021-12-20 14:59 **/ public class PackageImportTest { public static void main(String[] args) { Bank bank = new Bank(); Arrays.toString(new int[]{1, 2, 3}); HashMap map = new HashMap(); Scanner scanner = null; Account acct = new Account(1000); //全类名的方式显示 com.atguigu.exer3.Account acct1 = new com.atguigu.exer3.Account(1000,2000,0.0123); Date date = new Date(); java.sql.Date date1 = new java.sql.Date(123465l); Dog dog = new Dog(); //import static:导入指定类或接口中的静态结构:属性或方法 out.println("hello"); long num = round(81); } }
2021年12月19日
98 阅读
0 评论
0 点赞
1
2
...
4