【原】Java学习笔记020 - 面向对象
package cn.temptation;
public class Sample01 {
public static void main(String[] args) {
// 成员方法的参数列表:
// 1、参数列表中的数据类型是值类型
// 2、参数列表中的数据类型是引用类型
// A:一般的类:成员方法使用的该类的对象
Person person = new Person();
person.method(123);
TestPerson testPerson = new TestPerson();
Person personEx = new Person();
testPerson.test(personEx);
// 使用匿名对象作为方法的实参进行传递
testPerson.test(new Person());
}
}
class Person {
// 成员方法
public void method(int i) {
System.out.println("i的值为:" + i);
}
}
class TestPerson {
// 成员方法
public void test(Person person) {
person.method(999);
}
}
package cn.temptation;
public class Sample02 {
public static void main(String[] args) {
// 成员方法的参数列表:
// 1、参数列表中的数据类型是值类型
// 2、参数列表中的数据类型是引用类型
// B:抽象类:成员方法使用的是该抽象类的具体实现子类的对象
TestAnimal testAnimal = new TestAnimal();
// 语法错误:Cannot instantiate the type Animal
// testAnimal.test(new Animal());
// 多态
Animal animal = new Dog();
testAnimal.test(animal);
// 使用匿名对象作为实参进行传递
// 下面两句均正确
testAnimal.test(new Dog());
testAnimal.test((Animal)(new Dog()));
Dog dog = new Dog();
testAnimal.test(dog);
testAnimal.testEx(dog);
// The method testEx(Dog) in the type TestAnimal is not applicable for the arguments (Animal)
// testAnimal.testEx(animal);
// 注意:
// 1、继承关系中的子类类型出现在父类对象出现的场合,可以代替父类对象
// 2、继承关系中的父类类型出现在子类对象出现的场合,不可以代替子类对象
}
}
// 抽象类
abstract class Animal {
public abstract void eat();
}
// 具体实现子类
class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
class TestAnimal {
// 成员方法
public void test(Animal animal) {
animal.eat();
}
public void testEx(Dog dog) {
dog.eat();
}
}
package cn.temptation;
public class Sample03 {
public static void main(String[] args) {
// 成员方法的参数列表:
// 1、参数列表中的数据类型是值类型
// 2、参数列表中的数据类型是引用类型
// C:接口:成员方法使用的是该接口的实现类的对象
TestSport testSport = new TestSport();
// 语法错误:Cannot instantiate the type Sport
// testSport.test(new Sport());
// 多态
Sport sport = new Sporter();
testSport.test(sport);
// 使用匿名对象作为实参进行传递
testSport.test(new Sporter());
}
}
interface Sport {
public abstract void swim();
}
class Sporter implements Sport {
@Override
public void swim() {
System.out.println("学会了游泳");
}
}
class TestSport {
public void test(Sport sport) {
sport.swim();
}
}
package cn.temptation;
public class Sample04 {
public static void main(String[] args) {
// 成员方法的返回值类型:
// 1、返回值的数据类型是值类型
// 2、返回值的数据类型是引用类型
// A:一般的类:返回的是该类的对象
Man man = new Man();
double income = man.income(10000);
TestMan testMan = new TestMan();
System.out.println(testMan.test());
}
}
class Man {
public double income(int money) {
System.out.println("赚了钱要上交");
return money * 0.9;
}
}
class TestMan {
public Man test() {
// 返回引用数据类型的默认值null
// return null;
// 返回Man类类型的对象
// Man man = new Man();
// return man;
// 返回匿名对象
// return (new Man());
return new Man();
}
}
package cn.temptation;
public class Sample05 {
public static void main(String[] args) {
// 成员方法的返回值类型:
// 1、返回值的数据类型是值类型
// 2、返回值的数据类型是引用类型
// B:抽象类:返回的是该抽象类的具体实现子类的对象
TestHuman testHuman = new TestHuman();
Human human = testHuman.test();
human.live();
}
}
abstract class Human {
public abstract void live();
}
class Chinese extends Human {
@Override
public void live() {
System.out.println("天朝的人过着苦逼的生活");
}
}
class TestHuman {
public Human test() {
// 抽象类不能实例化
// Human human = new Human();
// return human;
// 多态
// Human human = new Chinese();
// return human;
// 返回匿名对象
// return (new Chinese());
return new Chinese();
}
}
package cn.temptation;
public class Sample06 {
public static void main(String[] args) {
// 成员方法的返回值类型:
// 1、返回值的数据类型是值类型
// 2、返回值的数据类型是引用类型
// C:接口:返回的是该抽象类的具体实现子类的对象
TestAbility testAbility = new TestAbility();
Ability ability = testAbility.test();
ability.fly();
}
}
interface Ability {
public abstract void fly();
}
class Phoenix implements Ability {
@Override
public void fly() {
System.out.println("凤凰会飞");
}
}
class TestAbility {
public Ability test() {
// 接口不能实例化
// Ability ability = new Ability();
// return ability;
// 多态
// Ability ability = new Phoenix();
// return ability;
// 匿名接口的实现类对象
// return (new Phoenix());
return new Phoenix();
}
}
package cn.temptation;
public class Sample07 {
public static void main(String[] args) {
TestGirl testGirl = new TestGirl();
// Girl girl = testGirl.test();
// girl.show();
// 链式编程写法:调用某一个成员方法,获得的是一个对象,自然就可以再使用这个对象的成员方法
testGirl.test().show();
// 链式编程写法结合匿名对象的使用
(new TestGirl()).test().show();
// 链式编程写法 和 匿名对象写法的区别:
// 1、链式编程写法:调用的都是方法;匿名对象写法需要new 某一个类的构造函数来获取该类的实例对象
// 2、链式编程写法通过方法的调用得到一个对象,再使用该对象的成员方法,最终使用的是一个对象的成员方法;匿名对象获取到的是一个对象
}
}
class Girl {
public void show() {
System.out.println("妹纸负责貌美如花");
}
}
class TestGirl {
public Girl test() {
return new Girl();
}
}
package cn.temptation;
public class Sample08 {
public static void main(String[] args) {
// Student student1 = new Student();
// Student student2 = new Student();
// 对于上述的代码的写法,我们清楚的知道,对象的创建都是有资源的消耗的(堆内存中开辟了空间)
// 当这些对象不再使用时,需要被销毁,Java中不需要开发人员进行手工销毁,提供了对不再使用的对象自动销毁的机制,称为垃圾回收机制(GC:Garbage Collection)
// 这个GC并不是实时进行的,而是要等到垃圾回收器空闲时才会来进行销毁,所以可以看出,在程序中漫无目的的创建对象对程序的性能有影响
// 所以,也就考虑在程序中,只需要使用一个对象时,就创建一个对象并在程序执行的过程中就保持只有一个对象
// "单个实例对象问题"在软件开发的过程中是一个常见的问题,也曾经困扰过开发人员,在长期的软件开发过程中,开发人员针对这些有代表性的问题积累了解决这些问题的经验
// 对这些经验的归纳总结,形成了大家都认可的设计方案,称为"设计模式"(Design Pattern)
// 设计模式常见的有23种,大家对于设计模式的理解和掌握希望遵循这样的原则:不要为了使用设计模式而使用设计模式
// 好的程序、好的代码都是反复迭代出来的,没有一蹴而就的;只有更适合业务的设计,没有最通用的设计
// 【单例模式】:Singleton,保证程序执行过程中有且仅有一个对象(单例)
// 【单例模式的实现方式1、饿汉式】:伴随着类的加载,就进行类的实例化,创建出该类的对象出来
// 随意new出对象的方式
// Singleton singleton1 = new Singleton();
// Singleton singleton2 = new Singleton();
// System.out.println("singleton1:" + singleton1);
// System.out.println("singleton2:" + singleton2);
// 第2次迭代写法的调用
// Singleton singleton1 = Singleton.getInstance();
// Singleton singleton2 = Singleton.getInstance();
// System.out.println("singleton1:" + singleton1);
// System.out.println("singleton2:" + singleton2);
// 第3次迭代写法的调用
// System.out.println(Singleton.instance);
// System.out.println(Singleton.instance);
// 第4次迭代写法的调用
// Singleton singleton1 = Singleton.getInstance();
// Singleton singleton2 = Singleton.getInstance();
// System.out.println("singleton1:" + singleton1);
// System.out.println("singleton2:" + singleton2);
}
}
// 学生类
//class Student {
//
//}
//class Singleton {
// // 成员变量
// // 【第3次迭代】从静态的成员得到启示,考虑使用static关键字的成员都是伴随着类的加载而加载的,且只执行一次
// // 这样写,效果上达到了执行过程中只有单个实例对象,但是使用public暴露静态的成员变量给外部使用不太好
//// public static Singleton instance = new Singleton();
//
// // 【第4次迭代】
// private static Singleton instance = new Singleton();
//
// // 构造函数
// // 【第1次迭代】构造函数设置为private,让随意new成为不可能
// private Singleton() {
//
// }
//
// // 成员方法
// // 【第1次迭代】构造函数设置为private不能new出对象,类似于类中的成员变量的数据保护处理方式,考虑创建一个外部可以访问的方法来获取Singleton类类型的对象
//// public Singleton getInstance() {
//// // 考虑到在Singleton类的外部无法访问private修饰的构造函数,但是在类的成员方法中还是可以访问
//// Singleton singleton = new Singleton();
//// return singleton;
//// }
//
// // 【第2次迭代】让外部可以调用到获取实例的方法,像第1次迭代里写的必须要使用对象名.成员方法才是使用,但是对象都创建不出来,怎么获取对象名呢?
// // 让外部可以调用到获取实例的方法,应该通过类名.成员方法才合适
// // 但是这样写还是有问题的,因为在成员方法的内部每次方法被调用,都会创建出一个新的Singleton类型的对象返回
//// public static Singleton getInstance() {
//// Singleton singleton = new Singleton();
//// return singleton;
//// }
//
// // 【第4次迭代】
// public static Singleton getInstance() {
// return instance;
// }
//}
package cn.temptation;
public class Sample09 {
public static void main(String[] args) {
// 【单例模式的实现方式2、懒汉式】:第一次使用类的对象时,才进行类的实例化,后续都是使用第一次创建出来的对象实例
Singleton singleton1 = Singleton.getInstance();
System.out.println("singleton1:" + singleton1);
Singleton singleton2 = Singleton.getInstance();
System.out.println("singleton2:" + singleton2);
}
}
class Singleton {
// 成员变量
// 懒汉式不需要随着类的加载就创建类的实例对象,所以只定义成员变量不做初始化操作
private static Singleton instance;
// 构造函数
// 构造函数设置为private,让随意new成为不可能
private Singleton() {
}
// 成员方法
public static Singleton getInstance() {
if (instance == null) { // 说明当前程序中没有Singleton类类型的对象
// 因为类加载时没有立即创建该类的对象,所以第一次调用getInstance方法创建该类的对象时,这个静态变量instance的值为默认值null
// 第一次创建该类的对象时需要进行类的初始化
instance = new Singleton();
}
// 后续使用时,都是使用第一次创建出来的对象实例
return instance;
}
}
package cn.temptation;
public class Sample10 {
public static void main(String[] args) {
// 设计模式中的模板方法模式(Template Method)
Shape shape1 = new Matrix(2, 3);
shape1.print();
Shape shape2 = new Circle(4);
shape2.print();
}
}
// 抽象类:形状
abstract class Shape {
// 思考:为什么没有定义成员变量?
// 答:因为计算不同的形状需要的参数是不同的,在抽象类中不定死成员方法的参数列表,而让各个继承的具体实现子类自由设置
// 成员变量
// 构造函数
// 成员方法
// 抽象的成员方法:求面积
public abstract double getArea();
// 抽象的成员方法:求周长
public abstract double getLength();
// 非抽象的成员方法:提供一个打印方法
public void print() {
System.out.println("面积为:" + getArea() + ",周长为:" + getLength());
}
}
// 具体实现子类:矩形
class Matrix extends Shape {
// 成员变量
// 长
private int i;
// 宽
private int j;
// 构造函数
public Matrix() {
}
public Matrix(int i, int j) {
super();
this.i = i;
this.j = j;
}
// 成员方法
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
// 自定义的成员方法(重写)
@Override
public double getArea() {
return this.i * this.j;
}
@Override
public double getLength() {
return 2 * (this.i + this.j);
}
}
// 具体实现子类:圆形
class Circle extends Shape {
// 成员变量
// 半径
private int k;
// 构造函数
public Circle() {
super();
}
public Circle(int k) {
super();
this.k = k;
}
// 成员方法
public int getK() {
return k;
}
public void setK(int k) {
this.k = k;
}
// 自定义的成员方法(重写)
@Override
public double getArea() {
return Math.PI * this.k * this.k;
}
@Override
public double getLength() {
return 2 * Math.PI * this.k;
}
}
package cn.temptation;
public class Sample11 {
public static void main(String[] args) {
/*
* 权限修饰符的总结:
* |(同一包下)当前类(本类) | (同一包下)子类或无关类 |(不同包下)子类 |(不同包下)无关类
* public(公有) | √ | √ | √ | √
* protected(受保护的) | √ | √ | √ | ×
* default(默认) | √ | √ | × | ×
* private(私有) | √ | × | × | ×
*/
Sample11 sample11 = new Sample11();
sample11.method1();
sample11.method2();
sample11.method3();
sample11.method4();
}
public void method1() {
System.out.println("public修饰的方法");
}
protected void method2() {
System.out.println("protected修饰的方法");
}
void method3() {
System.out.println("默认修饰的方法");
}
private void method4() {
System.out.println("private修饰的方法");
}
}
package cn.temptation;
public class Sample12 extends Sample11 {
public static void main(String[] args) {
Sample11 sample11 = new Sample11();
sample11.method1();
sample11.method2();
sample11.method3();
// 父类的私有成员方法不能访问
// 语法错误:The method method4() from the type Sample11 is not visible
// sample11.method4();
Sample12 sample12 = new Sample12();
sample12.method1();
sample12.method2();
sample12.method3();
// 父类的私有成员方法不能访问
// 语法错误:The method method4() from the type Sample11 is not visible
// sample12.method4();
}
}
package cn.temptation;
public class Sample13 {
public static void main(String[] args) {
Sample11 sample11 = new Sample11();
sample11.method1();
sample11.method2();
sample11.method3();
// Sample11类的私有成员方法不能访问
// 语法错误:The method method4() from the type Sample11 is not visible
// sample11.method4();
}
}
package jp.temptation;
import cn.temptation.Sample11; // 导入不同的包
public class Sample14 extends Sample11 {
public static void main(String[] args) {
Sample11 sample11 = new Sample11();
sample11.method1();
// 语法错误:The method method2() from the type Sample11 is not visible
// sample11.method2();
// 语法错误:The method method3() from the type Sample11 is not visible
// sample11.method3();
// 语法错误:The method method4() from the type Sample11 is not visible
// sample11.method4();
// 注意:
// 不同包下的子类可以调用自己本类继承而来的protected成员方法
// 不同包下的子类中创建的父类对象不能调用自己的protected成员方法
Sample14 sample14 = new Sample14();
sample14.method1();
sample14.method2();
// 语法错误:The method method3() from the type Sample11 is not visible
// sample14.method3();
// 语法错误:The method method4() from the type Sample11 is not visible
// sample14.method4();
}
}
package jp.temptation;
import cn.temptation.Sample11;
public class Sample15 {
public static void main(String[] args) {
Sample11 sample11 = new Sample11();
sample11.method1();
// 语法错误:The method method2() from the type Sample11 is not visible
// sample11.method2();
// 语法错误:The method method3() from the type Sample11 is not visible
// sample11.method3();
// 语法错误:The method method4() from the type Sample11 is not visible
// sample11.method4();
}
}
package cn.temptation; // 语法错误:Illegal modifier for the class Sample16; only public, abstract & final are permitted
//protected class Sample16 {
public class Sample16 {
// 成员变量
public int i = 2;
protected int j = 3;
int k = 4;
private int x = 5; public static int y = 6;
public final int z = 7; // 语法错误:Illegal modifier for the field m; only public, protected, private, static, final, transient & volatile are permitted
// public abstract int m = 8; // 构造函数
// public Sample16() {}
// protected Sample16() {}
// Sample16() {}
// private Sample16() {} // 语法错误:Illegal modifier for the constructor in type Sample16; only public, protected & private are permitted
// public static Sample16() {}
// 语法错误:Illegal modifier for the constructor in type Sample16; only public, protected & private are permitted
// public final Sample16() {} // 语法错误:Illegal modifier for the constructor in type Sample16; only public, protected & private are permitted
// public abstract Sample16() {} public static void main(String[] args) {
/*
* 修饰符的总结
*
* 权限修饰符
* 1)public
* 2)protected
* 3)default(默认)
* 4)private
*
* 状态修饰符
* 1)static
* 2)final
*
* 抽象修饰符
* 1)abstract
*
* ---------------------------------------------------
*
* 类:
* 权限修饰符:public、默认
* 状态修饰符:final
* 抽象修饰符:abstract
*
* 成员变量:
* 权限修饰符:public、protected、默认、private
* 状态修饰符:均可
* 抽象修饰符:不可
*
* 构造函数:
* 权限修饰符:public、protected、默认、private
* 状态修饰符:不可
* 抽象修饰符:不可
*
* 成员方法:
* 权限修饰符:public、protected、默认、private
* 状态修饰符:均可
* 抽象修饰符:一旦使用了抽象修饰符则成员方法所在的类也必须是抽象的
*
*/
}
}
【原】Java学习笔记020 - 面向对象的更多相关文章
- Java学习笔记之---面向对象
Java学习笔记之---面向对象 (一)封装 (1)封装的优点 良好的封装能够减少耦合. 类内部的结构可以自由修改. 可以对成员变量进行更精确的控制. 隐藏信息,实现细节. (2)实现封装的步骤 1. ...
- Java学习笔记之面向对象、static关键字
一周Java学习总结 今天就总结理清一下关于面向对象和面向过程的程序设计的一些不同特点,以及讲下static关键字. 面向对象 现在接触的Java是面向对象的,现在的程序开发几乎都是以面向对象为基础的 ...
- Java 学习笔记(4)——面向对象
现在一般的语言都支持面向对象,而java更是将其做到很过分的地步,java是强制使用面向对象的写法,简单的写一个Hello Word都必须使用面向对象,这也是当初我很反感它的一点,当然现在也是很不喜欢 ...
- 【原】Java学习笔记019 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 仔细想一想,Ani ...
- 【原】Java学习笔记016 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // this 关键字 ...
- 【原】Java学习笔记014 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 面向对象思想 // ...
- 【原】Java学习笔记018 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 继承关系的子类可以 ...
- 【原】Java学习笔记017 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 继承关系中的pri ...
- 【原】Java学习笔记015 - 面向对象
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 传递 值类型参数 ...
随机推荐
- SpringBoot环境搭建
创建 maven 项目 , 选择的打包类型为 jar 类型 自己构建 SpringBoot 项目时 , 要继承 SpringBoot 的父项目 , 这里用的版本是 2.1.4 点击 Finish , ...
- Python爬虫入门教程 16-100 500px摄影师社区抓取摄影师数据
写在前面 今天要抓取的网站为 https://500px.me/ ,这是一个摄影社区,在一个摄影社区里面本来应该爬取的是图片信息,可是我发现好像也没啥有意思的,忽然觉得爬取一下这个网站的摄影师更好玩一 ...
- C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解
C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...
- [Leetcode]100. Same Tree -David_Lin
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【Java并发编程】Callable、Future和FutureTask的实现
启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...
- [一]FileDescriptor文件描述符 标准输入输出错误 文件描述符
文件描述符 当应用程序请求打开或者操作文件时,操作系统为应用程序设置一张文件列表,具体的实现形式此处不深入说明 操作系统会提供给你一个非负整数,作为一个索引号,它的作用就像地址或者说指针或者说偏移 ...
- [转]gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例)
本文转自:http://www.fayfox.com/post/39.html 首先祭出官网文档链接:https://docs.gitlab.com/omnibus/settings/smtp.htm ...
- jQuery点击图片放大拖动查看效果
效果如图: 放大前: 放大后(可拖动图片浏览): 源码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- [Go] golang创建目录写文件判断文件
package main import ( "log" "os" ) func main() { //创建目录 os.Mkdir("test" ...
- [Go] golang结构体成员与函数类型
package main import ( "fmt" ) //定义一个类型 type tsh struct { //定义成员,类型是func() string test func ...