以类的方式组织代码,以对象的组织封装数据
- 抽象
- 三大特性
- 封装
- 继承
- 多态
- 先有对象,后有类
package com.oop.Demo01; import java.io.IOException; // Demo01 类 public class Demo01 { //main 方法 public static void main(String[] args) { } // return 返回一个值,结束类 public String sayHello(){ return "hello world"; } public int Max(int a,int b){ return a>b?a:b;//三元运算符 } public void readFile(String file) throws IOException{ } }
package com.oop.Demo01; public class Demo02 { public static void main(String[] args) { //实例化这个类 //对象类型 对象名= 对象值 student student=new student(); student.say(); } //和类一起加载的 public static void a(){ // b(); } // 类实例化之后才存在 public void b(){ } } package com.oop.Demo01; public class student { //非静态方法 public static void say(){ System.out.println("学生说话了"); } }
package com.oop.Demo01; public class Demo03 { public static void main(String[] args) { // 实际参数和形式参数的类型对应 int add =Demo03.add(1,2); System.out.println(add); } public static int add(int a,int b){ return a+b; } }
package com.oop.Demo01; // 值传递 public class Demo04 { public static void main(String[] args) { int a=1; System.out.println(a); Demo04.changs(a); System.out.println(a); } public static void changs(int a){ a=10; } }
package com.oop.Demo01; //引用传递:对象,本质还是值传递 public class Demo05 { public static void main(String[] args) { person person = new person(); System.out.println(person.name); Demo05.change(person); System.out.println(person.name); } public static void change(person person){ //person 是一个对象:指向 person person = new person(); 这是一个具体的人,可以改变属性 person.name="split"; } } // 定义一个person类,有一个属性:name class person{ String name; //null }类和对象的创建
-
类是一种抽象的数据类型,他是对某一类事物整体描述/定义,但是并不是代表某一个具体的事物
-
对象是抽象概念的具体实例
-
使用new关键词创建对象
package com.oop.Demo02; // 一个项目应该只有一个main方法 public class Application { public static void main(String[] args) { //类,抽象的,实例化 //类实例化后悔返回一个自己的对象 //student对象就是一个student类的具体实例 student xiaohong= new student(); student xiaoming =new student(); xiaoming.name= "小明"; xiaoming.age=2; xiaohong.name= "小红"; xiaohong.age=2; System.out.println(xiaoming.name); System.out.println(xiaoming.age); } }
package com.oop.Demo02; //学生类 public class student { //属性 String name; int age; //方法 public void study(){ System.out.println(this.name+"在学习"); } }构造器
-
必须和类的名字相同
-
必须没有返回类型,也不能写void
-
注意点
- 定义有参构造之后,如果想使用无参构造,现实的定义一个无参的构造
package com.oop.Demo02; public class Application_person { public static void main(String[] args) { //new 实例化一个对象 person person = new person(); } }
package com.oop.Demo02; public class person { //一个方法即使什么都不写,他也会存在一个方法 //显示的定义构造器 String name; //实例化初始值 //1.使用new 关键字,本质是调用构造器 //2.用来初始化值 //alt+insert public person(){ this.name="split"; } public person(String name){ this.name=name; } }
package com.oop.Demo03; public class Application_pet { public static void main(String[] args) { pet dog =new pet(); dog.age=2; dog.name="正顺"; dog.shout(); pet cat =new pet(); cat.age=2; cat.name="正顺"; System.out.println(dog.name); System.out.println(dog.age); } }
package com.oop.Demo03; public class pet { String name; int age; public void shout(){ System.out.println("叫了一声"); } }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TbvtlnGp-1636289651543)(D:我的文档我的图片Screenshots内存.png)]
小结类和对象- 类是一个模板,对象是一个具体的实例
- 方法:定义,调用
- 对应的引用
- 引用类型:基本类型
- 对象是通过引用来 *** 作的
- 修饰符 属性类型 属性名=属性值
- 对象的使用
- 对象的属性:split.name
- 对象的方法:split.sleep();
- 类:
静态的属性:属性
动态的行为:方法
封装- 高内聚,低耦合
- 高内聚:类的内部数据 *** 作细节自己完成,不允许外部干涉
- 低耦合:仅仅暴露 少量的方法给外部使用
-
属性私有,get/set
-
特点
- 提高了程序的安全性,保护数据
- 隐藏代码的时限细节
- 统一接口
- 系统可维护嶒加了
package com.oop.Demo04; public class student { //属性私有 //名字 private String name; //学号 private int id; //性别 private char sex; private int age; //提供一些可以 *** 作这个属性的方法 //提供public 的get,set方法 //get获得这个数据 public String getName(){ return this.name; } //set 给这个数据设置值 public void setName(String name){ this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { if (age>=0&&age<100){ this.age = age; } else { this.age=3; } } }
package com.oop.Demo04; public class Application_student { public static void main(String[] args) { student s1=new student(); s1.setName("split"); System.out.println(s1.getName()); s1.setAge(99); System.out.println(s1.getAge()); } }继承
-
继承的本质是对某一批类的抽象,从而时限对现实世界的更好建模
-
extent的意思是拓展,子类是父类的拓展
-
JAVA中类只有单继承,没有多继承
-
子类继承了父类,会拥有父类的前部方法
-
- public公共的
- private私有的
- default默认的
- protected受保护的
-
Ctrl+H
- 注意点
- super调用父类的构造方法,必须在构造方法的第一个
- super必须只能出现在子类的方法或者构造方法中
- super和this不能同时调用构造方法
- VS this:
- 代表的对象不同
this:本身调用者这个对象
super:代表父类对象的应用
- 前提
this:没有继承也可以使用
super:只能在继承条件才可以使用
- 构造方法
this();本类的构造
super();父类的构造
package com.oop.Demo05; // 父类 public class person { public person(){ System.out.println("父类无参构造执行了"); } private int monet=1000000000; public void say(){ System.out.println("说了一句话"); } public int getMonet() { return monet; } public void setMonet(int monet) { this.monet = monet; } protected String name = "split"; public void print(){ System.out.println("Person"); } }
package com.oop.Demo05; public class student extends person { public String name="hjc"; public student() { //隐藏代码:调用了父类的无参构造 super();//调用父类的构造器,必须在代码的第一行 System.out.println("子类无参构造执行了"); } public void print(){ System.out.println("shuo"); } public void text(String name){ System.out.println(name); System.out.println(this.name); System.out.println(super.name); } public void text2(){ print(); this.print(); super.print(); } }
package com.oop.Demo05; public class Application { public static void main(String[] args) { student student=new student(); // student.say(); // student.text("wuming"); // student.text2(); } }方法重写
-
重写都是方法的重写,和属性无关
-
需要有继承关系,子类重写父类的方法;
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大 public>protected>Default>private
- 抛出的异常,范围可以被缩小,不可以扩大。ClassNOtFoundException—>Exception(大)
-
重写,子类的方法和父类必须一致,方法体不同
-
为什么需要重写
- 父类的功能,子类不一定需要,或者不一定不满足
Alt+Insert–>override
多态-
可拓展
-
注意事项
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系。类型转换异常ClassCastException
- 存在条件:继承关系。方法需要重写。父类引用指向子类对象father f1 = new son();
- 不能重写
- 静态方法:
- static 方法 属于类,他不属于实例
- final 常量
- private 方法:私有的
代码>
package com.oop.Demo06; public class Application { public static void main(String[] args) { //一个对象的实际类型是确定的 //new Person(); //new Student(); //可以指向的引用类型就不是确定的 //父类的引用指向了子类 //Student能调用的方法啊都是自己的或者继承父类的 //Person父类型,可以指向子类,但是不调用子类独有的方法 Student s1= new Student(); Person s2= new Student(); Object s3= new Student(); //对象能执行哪些方法,主要看对象的类型,和右边关系不大 s2.run();//子类重写父类方法,执行子类方法 s1.run(); } }
父类>
package com.oop.Demo06; public class Person { public void run(){ System.out.println("run"); } }
子类>
package com.oop.Demo06; public class Student extends Person { @Override public void run() { System.out.println("son"); } }instanceof
- A instanceof B 两个类是否有关系
package com.oop.Demo06; public class Application { public static void main(String[] args) { Object object =new Student(); System.out.println(object instanceof Student); System.out.println(object instanceof Person); System.out.println(object instanceof teacher); System.out.println(object instanceof String); System.out.println("-----------------------------"); Person person =new Student(); System.out.println(person instanceof Student); System.out.println(person instanceof Person); System.out.println(person instanceof teacher); System.out.println(person instanceof Object); } }类型转换
- 父级引用指向子类的对象
- 把子类转换为父类,向上转换
- 把父类转换为子类,向下转换,强制转换
- 方便方法的调用,减少重复的代码,简介
package com.oop.Demo06; public class Application { public static void main(String[] args) { //类型的转化 父 子 //高=>低 Person obj = new Student(); // student 将这个对象转换成Student类型,我们就可以使用Student类型的方法 ((Student) obj).go(); //子类转化为父类可能丢失自己本来的一些方法 } }static 关键字
package com.oop.Demo07; import com.oop.Demo06.Student; public class student { private static int age; //静态变量 private double score; //非静态变量 public static void main(String[] args) { student s1 = new student(); System.out.println(student.age); System.out.println(s1.age); System.out.println(s1.score); } }
package com.oop.Demo07; import com.oop.Demo06.Student; public class student { private static int age; //静态变量 private double score; //非静态变量 public static void go(){ } public void run(){ } public static void main(String[] args) { go(); } }
代码块
package com.oop.Demo07; public class person { { // 代码块(匿名) } static { //静态代码块 } }
package com.oop.Demo07; public class person { // 附初始值 { // 代码块(匿名) System.out.println("匿名代码块"); } // 只执行一次 static { System.out.println("静态代码块"); } public person() { System.out.println("构造方法"); } public static void main(String[] args) { person aa =new person(); System.out.println("----------------------"); person aaa =new person(); } }
package com.oop.Demo07; //静态导入包 import static java.lang.Math.random; import static java.lang.Math.PI; public class text { public static void main(String[] args) { System.out.println(random()); System.out.println(PI); } }抽象类
-
abstract修饰符可以用来修饰方法可以修饰类,如果修饰方法,那么该方法就是抽象方法,如果修饰类,那么该类就是抽象类。
-
抽象类可以没有抽象方法,但是有抽象方法的类一定要申明为抽象类
-
特点
- 不能new这个抽象类,只能靠子类去实现他,约束。
- 抽象方法必须在抽象类中
- 抽象类存在构造器
- 存在的意义 抽象出来 提高开发效率
-
只有规范,自己无法写方法,专业的约束,约束和实现的分离:面向接口编程。
-
普通类:只有具体实现
-
抽象类:具体实现和规范
-
接口的本质是契约
-
声明类的关键词是class,声明接口的关键词是interface
-
作用
- 约束
- 定义一些方法,让不同的人实现
- public abstract
- public static final
- 接口不能被实例化,接口没有构造方法
- implements 可以实现多个接口
- 必须要重写接口中方法
package com.oop.Demo09; // 类 可以实现接口 implements 接口 // 实现了接口的类,就必须要实现接口的方法 //多继承 public class UserServiceImpl implements UserService,TimeService { @Override public void add(String name) { } @Override public void delete(String name) { } @Override public void update(String name) { } @Override public void query(String name) { } @Override public void timer() { } }
package com.oop.Demo09; public interface UserService { // 接口中的所有定义其实都是抽象的public //接口都需要有实现类 void add(String name); void delete(String name); void update(String name); void query(String name); }
package com.oop.Demo09; public interface TimeService { void timer(); }内部类
package com.oop.Demo10; public class Outer { private int id=10; public void out(){ System.out.println("这是外部类的方法"); } class Inner{ public void in(){ System.out.println("这是内部类"); } public void getID(){ System.out.println(id); } } }
package com.oop.Demo10; public class Application { public static void main(String[] args) { //new Outer outer =new Outer(); // 通过这个外部类来实例化内部类 Outer.Inner inner= outer.new Inner(); inner.in(); inner.getID(); } }
一个JAVA类中可以有多个class类,但是只能有一个public class
package com.oop.Demo10; public class Outer { } //一个JAVA类中可以有多个class类,但是只能有一个public class class A{ }
局部内部类
package com.oop.Demo10; public class Outer { // 局部内部类 public void method(){ class Inner{ } } }
匿名内部类
package com.oop.Demo10; public class Text { public static void main(String[] args) { //没有名字初始化类,不用将实例保存到变量中 new Apple().eat(); } } class Apple{ public void eat(){ System.out.println("1"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)