封装
程序设计追求"高内聚,低耦合"
高内聚就是类的内部数据操作细节自己完成,不允许外部的干涉
低耦合就是仅暴露少量方法给外部使用
属性私有:get/set
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 getAge() {
return age;
}
public void setAge(int age) {
if (age > 120 || age < 0) {
this.age = 3;
} else {
this.age = age;
}
}
}
package com.oop.demo04;
//提高安全性,隐藏代码的实现细节,统一接口,系统的可维护性增加
public class Application {
public static void main(String[] args) {
Student s1= new Student();
s1.setName("chengle");
System.out.println(s1.getName());
s1.setAge(999);
System.out.println(s1.getAge());
}
}
继承
本质是对一批类的抽象,从而实现对显示世界更好的建模
extands的意思是拓展,子类是父类的拓展
java中只有单继承,没有多继承
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
}
}
package com.oop.demo05;
public class Teacher extends Person{
}
package com.oop.demo05;
//子类继承父类,就会拥有父类的全部方法
public class Student extends Person {
}
package com.oop.demo05;
//在java中,所有的类都默认直接或者间接继承Object类
public class Person /*extends Object*/{
public int money=10000000;
public void say(){
System.out.println("说了一句话");
}
}
super
注意点;1.调用父类的构造方法,必须是构造方法的第一个
2.只能出现在子类的方法或构造方法中
3.super和this不能同时调用构造方法
package com.oop.demo05;
//在java中,所有的类都默认直接或者间接继承Object类
public class Person /*extends Object*/{
public Person() {
System.out.println("Person无参构造执行了");
}
protected String name="chengke";
//如果是private,私有的东西无法被继承
public void print(){
System.out.println("Person");
}
}
package com.oop.demo05;
//子类继承父类,就会拥有父类的全部方法
public class Student extends Person {
public Student() {
System.out.println("Student无参构造执行了");
}
private String name = "lege";
public void test(String name) {
System.out.println(name);//程乐
System.out.println(this.name);//lege
System.out.println(super.name);//chengke
}
public void test1(){
print();//Student
this.print();//Student
super.print();//Person
}
public void print() {
System.out.println("Student");
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("程乐");
student.test1();
}
}
和this的区别:
1 .代表的对象不同:this:本身调用者这个对象
super;代表父类对象的应用
2 .前提不同:this:没有继承也可以使用
super:只能在继承方法中使用
3 .构造方法不同:this():本类的构造
super():父类的构造
方法的重写
重写必须有继承关系,子类重写父类的方法
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大,但是不能缩小 public>protected>default>private
4.抛出异常,范围可以缩小,但是不能扩大
重写:子类的方法和父类必须一致,但是方法体不同
package com.oop.demo06;
//继承
public class A extends B {
@Override
public void test() {
System.out.println("A=>test");
}
}
package com.oop.demo06;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test");
}
}
package com.oop.demo06;
public class Application {
public static void main(String[] args) {
//方法的调用只和左边有关,定义额度数据类型有关
//静态方法和非静态方法区别很大
A a = new A();
a.test();//A
//父类的引用指向了子类
B b =new A();
b.test();//B
}
}
为什么要重写?
1.父类的功能,子类不一定需要,或者不一定满足
alt+insert ; override;
多态:同一对象可以根据发送对象的不同而采用不同的行为方式
一个对象的类型是确定的,但是可以指向的引用类型有很多(父类,有关系的类)
多态的注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类有联系
3.存在条件:继承关系,重写方法,父类引用指向子类的对象 Father f1 =new Son();
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("ear");
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Aplication {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
Student s1 = new Student();
//父类型可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写了父类的方法,执行子类的方法
s1.run();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
s2.eat();//报红
s1.eat();
}
}
另一个例子如下:
public class Student extends Person {
}
public class Teacher extends Person {
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Aplication {
public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof String);//false
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof Object);//true
System.out.println("=============================");
Person person = new Student();
System.out.println(person instanceof Student);//true
//System.out.println(person instanceof String);//编译报错
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Teacher);//false
System.out.println(person instanceof Object);//true
System.out.println("=============================");
Student student = new Student();
System.out.println(student instanceof Student);//true
//System.out.println(student instanceof String);//编译报错
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);//编译报错
;
}
}
子类转化为父类,可能丢失一些自己的方法
父类的引用指向子类的对象
把子类转换为父类,向上转型
把父类转换为子类,向下转型;需要强制转换,可能会丢失方法
方便方法的·调用,减少重复的方法,简洁!
public class Aplication {
public static void main(String[] args) {
Person obj =new Student();
Student student=(Student)obj;
student.go();
}
}`
public class Student extends Person {
public void go(){
System.out.println("gogogo");
}
}
``
static关键字理解
最简单的使用
package com.oop.demo08;
//静态导入包
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
random();
}
}
静态与非静态的比较
public class Student {
private static int age;//静态变量
private double score;//非静态变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.score);
System.out.println(s1.age);
go();
// run();
}
}
static下输出的东西只会执行一次,第二次运行就不显示了
public class Person {
//2.赋初值
{
System.out.println("匿名代码块");
}
//1.只执行一次
static {
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("===================");
Person person2 = new Person();
}
}
抽象类
1.不能new这个抽象类,只能靠子类去实现它;约束
2.抽次类里面可以写普通方法
3.抽象方法必须在抽象类中
package com.oop.demo09;
//抽象类的所有方法,继承了它的子类,都必须实现它的方法~除非它的子类也是abstract
public class A extends Action {
@Override
public void dosomething() {
}
}
package com.oop.demo09;
//abstract抽象类 单继承
public abstract class Action {
//约束,有人帮我们实现
//abstract只有方法名字,没有方法的实现
public abstract void dosomething();
}
接口
普通类:只有具体实现
抽象类:具体实现和抽象方法(规范)都有
接口;只有规范,自己无法写方法
接口就是规范!
声明类的关键字是class,声明接口的关键字是interface
作用:1.约束
2.定义一些方法,让不同的人实现
3.public abstract
4.public static final
5.接口不能被实例化,接口中没有构造方法
6.可以实现多个,利用implements
7.必须要重写接口里面的方法
public interface UserService {
//常量 public static final
int age =99;
//接口中的所有定义都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
package com.oop.demo10;
//类可以实现一个接口
//实现了接口中的类,就要重写接口中的方法
//利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService{
public UserServiceImpl() {
super();
}
@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.demo10;
public interface TimeService {
void timer();
}
内部类
一个Java、类中可以有多个class类,但是只能有一个public class
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);
}
}
}
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.in();
inner.getID();
}
}
要看懂一些奇葩的类
public class Test {
public static void main(String[] args) {
//没有名字初始化类
new Apple().eat();
//new hello();报红
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService{
void hello();
}