目录
案例2:名字2-5,余额大于20,密码=6位;不符合谈提示设置默认值:
3.重载(overload)和重写(override)的区别:
appearance:设置外观
Color Scheme:设置外观
File Encodings:设置字体编码
src右键-->new -->package->输入名字com.xiaoming
同一个包不能有相同类名
java.lang.* //lang包是基本包,默认引入,不需要引入
java.util.* //util包系统提供的工具包,工具类,使用Scanner
java.awt.* //是Java的界面开发,GUI
java.net.* //网络包,网络开发
导入包:import java.lang.* 建议使用什么,导入什么 import java.util.Arrays;

修饰符可以修饰类,但是只有默认和public可以,其他的不行。
成员方法和成员方法的规则是一样的,
构造器搭配封装使用
年龄,必须在1-120,年龄,工资不能直接查看,name的长度在2-6字符之间
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Person person = new Person();
- person.setAge(19);
- person.setName("法外狂徒张三");
- person.job="律师";
- person.setSalary(12300);
- System.out.println(person.toString());
- }
- }
-
- class Person {
- private String name;
- private int age;
- public String job;
- private double salary;
-
- public Person(String name, int age, String job, double salary) {
- // this.name = name;
- // this.age = age;
- this.job = job;
- // this.salary = salary;
- setName(name);
- setAge(age);
- setSalary(salary);
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- if (name.length() >=1&&name.length()<=6){
- this.name = name;
- }else {
- System.out.println("名字太长了");
- this.name = "无名人";
- }
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- if (age >= 1 && age <= 120) {
- this.age = age;
- }else {
- System.out.println("年林不对我给你一个默认的18");
- this.age = 18;
- }
- }
-
- public double getSalary() {
- return salary;
- }
-
- public void setSalary(double salary) {
- this.salary = salary;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", job='" + job + '\'' +
- ", salary=" + salary +
- '}';
- }
- }
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Account account = new Account("张三",10,"2123");
- System.out.println(account.toString());
- }
- }
-
- class Account {
- private String name;
- private String password;
- private double balance;
-
- public Account(){
-
- }
-
- public Account(String name,double balance,String password){
- this.setBalance(balance);
- this.setName(name);
- this.setPassword(password);
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- if (name.length() < 5 && name.length() >= 2) {
- this.name = name;
- } else {
- System.out.println("姓名长度2-5");
- this.name = "无名";
- }
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- if (name.length() == 6) {
- this.password = password;
- } else {
- System.out.println("密码长度6");
- this.password = "000000";
- }
- }
-
- public double getBalance() {
- return balance;
- }
-
- public void setBalance(double balance) {
- if (balance > 20) {
- this.balance = balance;
- } else {
- System.out.println("余额大于20");
- }
-
- }
-
- @Override
- public String toString() {
- return "Account{" +
- "name='" + name + '\'' +
- ", password='" + password + '\'' +
- ", balance=" + balance +
- '}';
- }
- }
方便代码的复用性;把子类的共有属性抽离为一个接口
class 子类 extends 父类{}
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Pupil pupil = new Pupil();
- pupil.name ="银角大王";
- pupil.age = 11;
- pupil.setScore(60);
- pupil.showInfo();
-
- Graduate graduate = new Graduate();
- graduate.name ="金角大王";
- graduate.age = 20;
- graduate.setScore(70);
- graduate.showInfo();
- }
- }
-
- //是Pupil 和 Graduate的父类
- class Student{
- public String name;
- public int age;
- private double score;
-
- public void setScore(double score){
- this.score = score;
- }
-
- public void showInfo(){
- System.out.println("学生名"+name+"年龄"+age+"成绩"+score);
- }
- }
-
- class Pupil extends Student{
- public void testing(){
- System.out.println("小学生"+name+"正在考小学数学");
- }
- }
-
- class Graduate extends Student{
- public void testing(){
- System.out.println("大学生"+name+"正在考小学数学");
- }
- }
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Student student = new Student();
- student.showInfo();
- student.callTest();
- }
- }
-
- class Person{
- private String name;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- private void test(){
- System.out.println("私有方法你来调用我");
- }
-
- public void callTest(){
- this.test();
- }
- }
-
- class Student extends Person{
- public void showInfo(){
- setName("张三");
- System.out.println(getName());
- }
- }
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Student student = new Student("张三");
- System.out.println(student.name);
- }
- }
-
- class Person{
- public String name;
-
- public Person(String name) {
- this.name = name;
- }
- }
-
- class Student extends Person{
- public Student(String name) {
- super(name);
- }
- }

如果子类有这个属性就返回子类的,没有往上找,找到最上面都没有就报错。private定义属性肯定继承,就是访问不了,就近原则
案例1:看看输出什么。默认调用super
- public class User {
- public static void main(String[] args) {
- B b = new B();
- }
- }
-
- class A{
- A(){
- System.out.println("a");
- }
- A(String name){
- System.out.println("a name");
- }
- }
-
- class B extends A{
- B(){
- this("abc");
- System.out.println("b");
- }
- B(String name){
- System.out.println("b name");
- }
- }
用于访问父类的属性、方法、构造器。但不能访问私有的属性
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- new B().h1();
- }
- }
-
- class A{
- String name = "张三";
- private int age = 10;
-
- public A(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public void show(){
- System.out.println("来展示");
- }
- }
-
- class B extends A{
- public B() {
- super("李四", 19);
- }
-
- public void h1(){
- System.out.println(super.name);
- super.show();
- }
- }
super的访问适用于就近原则A->B->C.属性的覆盖
this与super的区别,this从子类开始找,super从父类开始找,this调用本类,super调用父类。this调用本类构造器,必须放在第一行。super调用父类构造器,必须放在第一行。
简单的来说:方法覆盖(重写)就是子类有一个方法,和父类的某个方法的名称,返回类型,参数一样,那么我么就说子类的方法覆盖了父类的方法。
- public class User {
- public static void main(String[] args) {
- new Dog().cry();
- }
- }
-
- class Animal{
- protected void cry(){ //父类访问权限,比子类小或者一样
- System.out.println("动物叫唤");
- }
- public Object bb(){
- return null;
- }
- }
-
- class Dog extends Animal{
- public void cry(){ //public->protected->默认->private
- System.out.println("小狗汪汪叫");
- }
-
- public String bb(){//子类的返回参数,是父类返回参数的子类或者相同
- return null;
- }
- }
重载 本类 方法名:必须一样 参数:个数,顺序至少一个不同包 返回类型:无要求 修饰符:无要求
重写 子父类 必须一样 相同 父类返回值相同,或父类子类 子类方法
编写一个person类,包括属性/private(name,age),构造器,方法say(返回自我介绍的字符串)。编写一个Student类,继承Person类,增加id,score属性/private,以及构造器,定义say方法(返回自我介绍的信息)。在main中分别创建Person和Student对象,调用say方法输出自我介绍。
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- Person person = new Person("jack",10);
- System.out.println(person.say());
-
- Student student = new Student("smith",20,123456,89);
- System.out.println(student.say());
- }
- }
-
- class Person {
- private String name;
- private int age;
-
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String say() {
- return "姓名" + name + "年龄" + 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;
- }
- }
-
- class Student extends Person {
- private int id;
- private double score;
-
- public Student(String name, int age, int id, double score) {
- super(name, age);
- this.id = id;
- this.score = score;
- }
-
- public String say() {
- return super.say() + "id=" + id + "score=" + score;
- }
- }
一个对象的编译理性和运行类型可以不一致。编译类型在定义对象时,就确定了,不能改变。运行类型是可以变化。编译类型看定义时 = 号的左边,运行类型看 = 号的 右边
多态的前提是:两个对象(类)存在继承关系。多态的向上转型
本质:父类的引用指向了子类的对象 语法: 父类类型 引用名 = new 子类类型();
特点:编译类型看左边,运行类型看右边。可以调用父类中的所有成员(但是要遵守访问权限),不能调用子类的特有成员。
- public class User {
- public static void main(String[] args) {
- Person person = new Student();
- person.hi();//向上转型只能访问父类有的方法与属性
- }
- }
-
- class Person{
- public void hi(){
- System.out.println("你好");
- }
- }
-
- class Student extends Person{
- public void hah(){
- System.out.println("hah");
- }
- }
语法:子类类型 引用名 = (子类类型) 父类引用。只能强转父类的引用,不能强转父类的对象。要求父类的引用指向的是当前目标类型的对象,可以调用子类类型中所有的成员。
- public class User {
- public static void main(String[] args) {
- Person person = new Student();
- person.hi();//向上转型只能访问父类有的方法与属性
-
- Student student = (Student) person;
- student.hah(); //向下转型就可以访问子类的方法与属性
- }
- }
-
- class Person {
- public void hi() {
- System.out.println("你好");
- }
- }
-
- class Student extends Person {
- public void hah() {
- System.out.println("hah");
- }
- }
定义:属性没有重写之说,属性看编译类型
instanceof比较操作符,用于判断莫某类型是不是某某类型或者是他的子类
- public class User {
- public static void main(String[] args) {
- //属性编译看左边哦
- Person person = new Student();
- System.out.println(person.count);
- }
- }
-
- class Person {
- int count = 10;
- }
-
- class Student extends Person {
- int count = 20;
- }
定义:当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定。当调用对象属性时,没有动态绑定机制,哪里声明,那里使用
应用实例:现有一个继承结构如下 Teacher(name,int,say()) Teacher-->teach() Student-->study() 遍历Teacher
使用:getClass()查看类
主要练习向上向下转型
- public class User {
- public static void main(String[] args) {
- Person[] people = new Person[5];
- people[0] = new Person("jack",20);
- people[1] = new Student("mary",21,99);
- people[2] = new Student("smith",19,66.5);
- people[3] = new Teacher("jan",80,20000);
- people[4] = new Teacher("wan",60,10000);
- for (int i = 0; i < people.length; i++) {
- System.out.println(people[i].toString());
- if (people[i] instanceof Student) ((Student) people[i]).study();
- if (people[i] instanceof Teacher) ((Teacher) people[i]).teacher();
- }
- }
- }
-
- class Person {
- public String name;
- public int age;
-
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
-
- class Student extends Person {
- double score;
-
- public Student(String name, int age, double score) {
- super(name, age);
- this.score = score;
- }
-
- public void study(){
- System.out.println("好好学习");
- }
-
- @Override
- public String toString() {
- return super.toString() + "学生成绩为" + score;
- }
- }
-
- class Teacher extends Person {
- double salary;
-
- public Teacher(String name, int age, double salary) {
- super(name, age);
- this.salary = salary;
- }
-
- public void teacher(){
- System.out.println("我要教学生");
- }
-
- @Override
- public String toString() {
- return super.toString()+"老师工资为17000";
- }
- }
1.定义:传递多态参数,就是传递父类的参数
- package com.user;
-
- public class User {
- public static void main(String[] args) {
- new Tools().show(new Person("张三",19));
- Person person = new Student("张三",19,10);
- new Tools().show(person);
- }
- }
-
- class Person{
- String name;
- int age;
-
- public Person(String name, int age) {
- this.name = name;
- this.age = age;
- }
- }
-
- class Student extends Person{
- int id;
- public Student(String name, int age,int id) {
- super(name, age);
- this.id = id;
- }
- }
-
- class Tools{
- public void show(Person p){
- if (p instanceof Person) System.out.println(p.name+p.age);
- if (p instanceof Student) System.out.println(p.name+p.age+((Student) p).id);
- }
- }
既可以判断基本类型,可以判断应用类型。如果判断基本类型,就是判断值是否相等。引用类型,判断的是地址是否相等
只能判断引用类型,默认是判断地址是否相等,子类中往往重写该方法,用于判断内容是否相等。
- package com.user;
-
- import java.util.Objects;
-
- public class User {
- public static void main(String[] args) {
- Person person1 = new Person("jack",10,'男');
- Person person2 = new Person("jack",10,'男');
- System.out.println(person2.equals(person1));
- }
- }
-
- class Person{
- String name;
- int age;
- char gender;
-
- public Person(String name, int age, char gender) {
- this.name = name;
- this.age = age;
- this.gender = gender;
- }
-
- //重写equals方法
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Person person = (Person) o;
- return age == person.age && gender == person.gender && Objects.equals(name, person.name);
- }
- }
提高具有哈希结构的容器效率!
两个引用,如果指向同一个对象,则哈希值肯定是一样的!
两个引用,如果指向的是不同对象,则哈希值是不一样的
哈希值主要是根据地址号的!,不能完全将hash值等价于地址
object.hashCode()。后面在集合中hashCode如果需要也会重写
1.默认返回全类名+@+hash值,所以一般我们也是重写哦
- public class User { public static void main(String[] args) {
- Person person = new Person();
- person.name ="张三";
- person.age = 10;
- System.out.println(person.toString());
- }
- }
-
- class Person{
- String name;
- int age;
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
1.定义:这个是垃圾回收器,系统会自动回收,如果程序员需要可以自己重写 System.gc()主动调用垃圾回收,
finalize几乎不用只是为了面试
F7(跳入) F8(跳出) shift+F8(跳出) F9(resume,执行到下一个断点)
右键运行有个debug运行那个就可以看效果
- public class User {
- public static void main(String[] args) {
- //debug 演示
- int sum = 0;
- for (int i = 0; i < 10; i++) {
- sum += i;
- System.out.println("i = " + i);
- System.out.println("sum = " + sum);
- }
- System.out.println("end...");
-
- int[] arr = new int[3];
- for (int i = 0; i < 4; i++) {
- arr[i] =1;
- }
- }
- }
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
-
- public class SmallChangeSys {
-
- //化繁为简
- // 1.先完成菜单,并可以选择菜单,给出对应提示信息
- // 2.完成零钱通明细 思路字符串
- public static void main(String[] args) {
- //定义相关变量
- Scanner scanner = new Scanner(System.in);
- boolean loop = true;
- String key = "";
-
- //2.完成零钱通
-
- String details = "---------零钱通明细--------", note, choice;
- //3.收益入账
- //定义新的变量
- double money, balance = 0;
- Date date = null;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm"); //格式化时间的格式
- do {
- System.out.println("========零钱通菜单========");
- System.out.println("\t\t\t1 零钱通明细");
- System.out.println("\t\t\t2 收益入账");
- System.out.println("\t\t\t3 消费");
- System.out.println("\t\t\t4 退 出");
-
- System.out.print("请选择(1-4): \t");
- key = scanner.next();
- switch (key) {
- case "1":
- System.out.println(details);
- break;
- case "2":
- System.out.println("收益入账金额:");
- money = scanner.nextDouble();
- //金额校验等下写
- balance += money;
- date = new Date(); //获取到当前的日期
- details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
- break;
- case "3":
- System.out.print("消费金额:\t");
- money = scanner.nextDouble();
- System.out.print("消费理由:\t");
- note = scanner.next();//消费理由
- //金额校验等下写
- balance -= money;
- date = new Date(); //获取到当前的日期
- details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
- break;
- case "4":
- System.out.print("你确定要退出吗?(y/n): \t");
- choice = scanner.next();
- if (choice.equals("n")) break;
- loop = false;
- break;
- default:
- System.out.println("选择有误,请重新选择");
- }
- } while (loop);
- System.out.println("退出了零钱通项目");
- }
- }
- package com.smallchange;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
-
- public class SmallChangeSys {
- public static void main(String[] args) {
- new SmallChangeSysOOP().meau();
- }
- }
-
- class SmallChangeSysOOP{
- boolean loop = true;
- String key = "";
- String details = "---------零钱通明细--------", note, choice;
- double money, balance = 0;
- Date date = null;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm");
- Scanner scanner = new Scanner(System.in);
-
- public void meau(){
- do {
- System.out.println("========零钱通菜单========");
- System.out.println("\t\t\t1 零钱通明细");
- System.out.println("\t\t\t2 收益入账");
- System.out.println("\t\t\t3 消费");
- System.out.println("\t\t\t4 退 出");
-
- System.out.print("请选择(1-4): \t");
- key = scanner.next();
- switch (key) {
- case "1":
- this.show();
- break;
- case "2":
- this.income();
- break;
- case "3":
- this.pay();
- break;
- case "4":
- this.exit();
- break;
- default:
- System.out.println("选择有误,请重新选择");
- }
- } while (loop);
- }
-
- public void show(){
- System.out.println(details);
- }
-
- public void income(){
- System.out.println("收益入账金额:");
- money = scanner.nextDouble();
- //金额校验等下写
- balance += money;
- date = new Date(); //获取到当前的日期
- details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
- }
-
- public void pay() {
- System.out.print("消费金额:\t");
- money = scanner.nextDouble();
- System.out.print("消费理由:\t");
- note = scanner.next();//消费理由
- //金额校验等下写
- balance -= money;
- date = new Date(); //获取到当前的日期
- details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
- }
-
- public void exit(){
- System.out.print("你确定要退出吗?(y/n): \t");
- choice = scanner.next();
- if (choice.equals("y"))loop = false;
- }
- }