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 void print(){
return;
}
public int max(int a,int b){
return a > b ? a : b;//三元运算符!
}
//数组下标越界:Arrayindexoutofbounds
public void readFile(String file) throws IOException{
}
}
package com.oop.demo01;
public class Student {
public void say(){
System.out.println("学生说话了");
}
}
package com.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student student = new Student();
student.say();
}
}
package com.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student student = new Student();
student.say();
}
/*
//和类一起加载的,无法调用b方法
public void a(){
b();
}
//类实例化 之后才存在
public void b(){
}
*/
public void a(){
b();
}
public void b(){
}
}
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.change(a);
System.out.println(a);//1
}
//返回值为空
public static void change(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.name = "bill";
}
}
//定义了一个Person类,有一个属性:name
class Person{
String name;
}
类与对象的关系
创建与初始化对象
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
//类:抽象的,实例化
//类实例化后会返回一个自己的对象!
//student对象就是一个Student类的具体实例!
Student student = new Student();
Student bill = new Student();
bill.name = "比尔";
bill.age = 10;
System.out.println(bill.name);
System.out.println(bill.age);
}
public class Student {
//属性:字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
构造器必须要掌握
类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下两个特点
package com.oop.demo02;
public class Person {
//一个类即使什么都不写,他也会存在一个方法
String name;
int age;
//Alt+Insert 快捷键可以帮助生成构造器
//1.使用new关键字,本质是在调用构造器
//2.构造器用来初始化值
//显示的定义构造器
public Person() {
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
/*
public static void main(String[] args) {
//new 实例化了一个对象
Person person = new Person("bil",23);
System.out.println(person.name);
System.out.println(person.age);
}
*/
package com.oop.demo02;
public class Pet {
public String name;
public int age;
public void shout(){
System.out.println("叫了一生");
}
}
/*
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 1;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
Pet cat = new Pet();
}
*/
封装
该露的露,该藏得藏
封装(数据得隐藏)
记住:属性私有,get/set
作用
package com.oop.demo03;
//类 private:私有
public class Student {
//属性私有
private String name; //名字
private int id;//学号
private char sex;//性别
private int age;//年龄
//提供一些可以操作这个属性的方法!
//提供一些public的get,set方法
//get 获得这个数据
public String getName() {
return name;
}
//给这个数据设置值
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 > 120 || age < 0){//不合法
this.age = 3;
}else {
this.age = age;
}
}
}
/*
Student s1 = new Student();
Student s2 = new Student();
s1.setName("bill");
s1.setId(1223);
s1.setAge(130);//不合法的
s2.setAge(20);
System.out.println(s1.getName());
System.out.println(s1.getId());
System.out.printl(s1.getAge());
System.out.println(s2.getAge());
*/
package com.oop.demo04;
//在Java中,所有的类,都默认直接或间接继承Object类
//Person 人:父类
public class Person {
//public
//protected
//default
//private
public int money = 10_0000_0000;
//private int money = 10;
public void say(){
System.out.println("say words");
}
/*
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
*/
}
package com.oop.demo04;
//Student is 人:子类,派生类
public class Student extends Person {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
}
}
package com.oop.demo04;
//Teacher is 人:子类,派生类
public class Teacher extends Person {
}
package com.oop.demo05;
public class Person {
//如果父类中重写了有参构造,没有写无参构造,子类则无法调用父类无参构造
public Person() {
System.out.println("Person无参执行了");
}
protected String name = "bill";
//private私有的东西无法被继承!
public void print(){
System.out.println("Person");
}
}
package com.oop.demo05;
public class Student extends Person {
public Student() {
//隐藏代码:调用了父类的无参构造,正常情况下不写,默认调用父类的无参构造
super();//调用父类的构造器,必须放在子类构造器的第一行
System.out.println("Student无参构造被执行了");
}
/*
public Student() {
//调用本身的构造器,也必须放在构造器的第一行
this.name("tom");
System.out.println("Student无参执行了");
}
public Student(String name) {
this.name = name;
}
*/
private String name = "wang";
public void print(){
System.out.println("Student");
}
public void test1(){
print();
this.print();
super.print();
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("bill1");
student.test1();
}
}
super注意点
VS this:
package com.oop.demo05;
//重写都是方法的重写,和属性无关
public class B {
public static void test(){//静态方法static
System.out.println("B=>test()");
}
}
package com.oop.demo05;
public class A extends B {
public static void test(){//静态方法static
System.out.println("A=>test()");
}
}
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
//方法的调用之和左边,定义的数据类型有关
A a = new A();
a.test();//A
//父类的引用指向了子类
B b = new A();
b.test();//B
}
}
/*
A=>test()
B=>test()
*/
package com.oop.demo05;
public class B {//非静态方法
public void test(){
System.out.println("B=>test()");
}
}
package com.oop.demo05;
public class A extends B //非静态方法
//Override:重写
@Override//注解:有功能的注释!
public void test() {
//super.test();默认调用父类的方法
System.out.println("A=>test()");//重写父类的方法
}
}
package com.oop.demo05;
public class Application {
//静态方法和非静态的方法区别很大!
//非静态:重写
public static void main(String[] args) {
A a = new A();
a.test();//A
B b = new A();//子类重写了父类的方法
b.test();//B
}
}
/*
A=>test()
A=>test()
*/
重写注意点:需要有继承关系,子类重写父类的方法!
重写,子类的方法和父类必要一致:方法体不同!
为什么需要重写:
多态注意事项:
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
package com.oop.demo06;
import com.sun.scenario.effect.impl.sw.java.JSWBlend_SRC_OUTPeer;
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
//Student(子类)能调用的方法都是自己或者继承父类的!
Student s1 = new Student();
//Person 父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
s2.run();//子类重写后,父类走子类的方法
s1.run();
s1.eat();
//s2.eat(); eat()方法子类独有,所以虽然父类指向子类,但依然不能调用
//((Student) s2).eat();类型转换:将s2 Person强制转换为Student
}
}
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
=====================================
package com.oop.demo06;
import com.sun.scenario.effect.impl.sw.java.JSWBlend_SRC_OUTPeer;
public class Student extends Person {
public void go(){
System.out.println("go");
}
}
=====================================
package com.oop.demo06;
public class Teacher extends Person {
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//Object > String
//Object > Person > Student
//Object > Person > Teacher
//System.out.println(X instanceof Y);//能不能编译通过主要看X,Y关系!
Object object = new Student();
System.out.println(object instanceof Student);//True
System.out.println(object instanceof Person);//True
System.out.println(object instanceof Object);//True
System.out.println(object instanceof Teacher);//False
System.out.println(object instanceof String);//False
System.out.println("==========");
Person person = new Student();
System.out.println(person instanceof Student);//True
System.out.println(person instanceof Person);//True
System.out.println(person instanceof Object);//True
System.out.println(person instanceof Teacher);//False
//System.out.println(person instanceof String);//编译报错!
System.out.println("==========");
Student student = new Student();
System.out.println(student instanceof Student);//True
System.out.println(student instanceof Person);//True
System.out.println(student instanceof Object);//True
//System.out.println(student instanceof Teacher);//编译报错!
//System.out.println(student instanceof String);//编译报错!
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
import com.oop.demo06.Teacher;
public class Application {
public static void main(String[] args) {
//类型之间的转换:父 子
//高 低
Person student = new Student();
//student 将这个对象转换为Student类型,就可以使用
((Student) student).go();
Student student1 = new Student();
student1.go();
//子类转换为父类,可能丢失自己的本来的一些方法!
Person person = student1;
//person.go();方法丢失
}
}
/*
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型
3.把父类转换为子类,向下转型;强制转换
4.方便方法的调用,减少重复的代码!简洁
*/
package com.oop.demo07;
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;
public class Student {
private static int age;//静态的变量 多线程
private double score;//非静态的变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
go();
//run();静态方法无法调用非静态方法
}
}
package com.oop.demo07;
public class Person {
/*
{
//代码块(匿名代码块)
}
static {
//静态代码块
}
*/
//2:赋初始值
{
System.out.println("匿名代码块");
}//1:只执行一次
static {
System.out.println("静态代码块");
}//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("=====");
Person person1 = new Person();
}
}
/*
静态代码块
匿名代码块
构造方法
=====
匿名代码块
构造方法
*/
package com.oop.demo07;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
package com.oop.demo08;
//abstract 抽象类:类 extends:单继承, (接口可以多继承)
public abstract class Action {
//约束,有人帮我们实现
//abstract,抽象方法,只有方法名字,没有方法的实现!
public abstract void doSomething();
//1.不能new这个抽象类,只能靠子类去实现它:约束!
//2.抽象类中可以写普通的方法
//3.抽象方法必须在抽象类中
//抽象的抽象;约束
}
package com.oop.demo08;
public class A extends Action {
@Override
public void doSomething() {
}
}
}
普通类:只有具体实现
抽象类:具体实现和规范(抽象方法)都有!
接口:只有规范!自己无法写方法专业的约束!约束和实现分离:面向接口编程
接口就是规范,定义的是一组规则,体现了现实世界中“如果你是...则必须能...”的思想。如果你是天使,则必须能飞。如果你是汽车,则必须能跑。如果你是好人,则必须干掉坏人。
接口的本质是契约,就像法律一样,制定好后需要大家都遵守
OO的精髓,是对对象的抽象,最能体现这一点的就是接口。我们讨论设计模式都只针对具备了抽象能力的语言(比如c++,java、c#等),因为设计模式所研究的,实际上就是如何合理的去抽象。
声明类的关键字是class,声明接口的关键字是interface
package com.oop.demo09;
// 抽象的思维~java
// interface 定义的关键字,接口都需要有实现类
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.demo09;
public interface TimeService {
void timer();
}
package com.oop.demo09;
//抽象类:extends~
//类 可以实现接口 implement 接口
//实现了接口的类,就需要重写接口中的方法~
//多继承~利用接口实现多继承
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.demo10;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
}
package com.oop;
import com.oop.demo10.Outer;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类~
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}
package com.oop.demo10;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public static class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
}
package com.oop.demo10;
public class Outer {
//局部内部类
public void method(){
class Inner{
public void in(){
}
}
}
}
package com.oop.demo10;
public class Test {
public static void main(String[] args) {
//没有名字初始化类,不用将实例保存到变量中~
new Apple().eat();
UserService userService = new UserService(){
@Override
public void hello() {
}
};
}
}
class Apple{
public void eat(){
System.out.println("1");
}
}
interface UserService{
void hello();
}
原文:https://www.cnblogs.com/wcwblog/p/14648814.html