首页
关于
友链
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工程
页面
关于
友链
搜索到
3
篇与
的结果
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 点赞
2021-12-13
java练习3
用栈进行括号匹配/** * @program: exercise * @description: 用栈进行括号匹配 * @author: yuuko * @create: 2021-12-12 15:42 **/ import java.util.Scanner; import java.util.Stack; public class KuoHao { public static void main(String[] args) { Scanner input=new Scanner(System.in); String s=input.next(); System.out.println(solution(s)); } public static boolean solution(String s){ Stack<Character> stack1=new Stack<>(); char currentCorrectChar; for(int i=0;i<s.length();i++) { switch (s.charAt(i)) { case '(': stack1.push(')'); break; case '[': stack1.push(']'); break; case '{': stack1.push('}'); break; default: if(stack1.isEmpty()) return false; else currentCorrectChar = stack1.pop(); if (currentCorrectChar != s.charAt(i)) return false; } } if(!stack1.isEmpty()) return false; return true; } }自定义栈进行括号匹配/** * @program: exercise * @description: 自己定义的栈 * @author: yuuko * @create: 2021-12-12 15:49 **/ public class myCharStack { private myCharStack header; private myCharStack next=null; private char content; myCharStack(){ this.header=this; } myCharStack(char content){ this.content=content; } public void push(char content){ myCharStack stack1=new myCharStack(content); stack1.next=this.header.next; this.header.next=stack1; } public char pop(){ if(this.header.next==null){ System.out.println("EmptyStackException here"); return 'X'; } char popContent=this.header.next.content; this.header.next=this.header.next.next; return popContent; } public boolean isEmpty(){ return this.header.next == null; } }/** * @program: exercise * @description: * @author: kkx * @create: 2021-12-12 15:57 **/ import java.util.Scanner; public class myKuoHao { public static void main(String[] args) { Scanner input=new Scanner(System.in); String s=input.next(); System.out.println(solution(s)); } public static boolean solution(String s){ myCharStack stack1=new myCharStack(); char currentCorrectChar; for(int i=0;i<s.length();i++) { switch (s.charAt(i)) { case '(': stack1.push(')'); break; case '[': stack1.push(']'); break; case '{': stack1.push('}'); break; default: currentCorrectChar = stack1.pop(); if (currentCorrectChar != s.charAt(i)) return false; } } if(!stack1.isEmpty()) return false; return true; } }
2021年12月13日
63 阅读
0 评论
0 点赞
2021-11-28
java练习2
看不懂的链表public class NodeTest { public static void main(String[] args) { Node node1 = new Node(1, "李白1", "刺客1"); Node node2 = new Node(2, "李白2", "刺客2"); Node node3 = new Node(3, "李白3", "刺客3"); Node node4 = new Node(4, "李白4", "刺客4"); Node node5 = new Node(5, "李白5", "刺客5"); Node node6 = new Node(5, "李白", "刺客5"); SingleLinkedList singleLinkedList = new SingleLinkedList(); singleLinkedList.add(node1); singleLinkedList.add(node2); singleLinkedList.add(node4); singleLinkedList.add(node5); singleLinkedList.add2(node3); singleLinkedList.delete(3); singleLinkedList.delete(6); singleLinkedList.change(node6); singleLinkedList.change(1, "李白6", "刺客6"); singleLinkedList.show(); } } class SingleLinkedList{ private Node head=new Node(0, "", ""); public void add(Node node){ Node temp = head; while (true){ if (temp.next == null){ break; } temp = temp.next; } temp.next = node; } public void add2(Node node){ Node temp = head; boolean flag = false; while (true){ if (temp.next == null){ break; } if (temp.next.no > node.no){ break; }else if (temp.next.no == node.no){ flag = true; break; } temp = temp.next; } if (flag){ System.out.println("编号已经存在,不能添加"); }else { node.next = temp.next; temp.next = node; } } public void delete(int no){ Node temp = head; boolean flag = false; while (true){ if (temp.next == null){ break; } if (temp.next.no == no){ flag = true; break; } temp = temp.next; } if (flag){ temp.next = temp.next.next; }else { System.out.println("不存在"); } } public void change(int no, String name, String type){ if(head.next == null){ System.out.println("链表为空"); return; } Node temp = head.next; boolean flag = false; while (true){ if(temp == null){ System.out.println("没有找到数据"); break; } if (temp.no == no){ flag = true; break; } } if (flag){ temp.name = name; temp.type = type; } } public void change(Node node){ Node temp = head; //boolean flag = false; delete(node.no); add2(node); } public void show(){ if (head.next == null){ System.out.println("该链表为空"); return; } Node temp = head.next; while (true){ if (temp == null){ break; } System.out.println(temp); temp = temp.next; } } } class Node{ public int no; public String name; public String type; public Node next; public Node(int no, String name, String type){ this.no = no; this.name = name; this.type = type; } @Override public String toString() { return "Node{" + "no=" + no + ", name='" + name + '\'' + ", type='" + type + '\'' + '}'; } }
2021年11月28日
37 阅读
0 评论
0 点赞