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
* 状态修饰符:均可
* 抽象修饰符:一旦使用了抽象修饰符则成员方法所在的类也必须是抽象的
*
*/
}
}

最新文章

  1. GridView的高度自适应
  2. 解决SVN不显示状态图标
  3. [OSG][转]osg格式文件
  4. Movie importing requires quicktime
  5. TreeMap实现原理
  6. A Tour of Go If and else
  7. iphone真机开发流程之--证书申请
  8. jquery中this与$this的区别
  9. C#.Net获取Mac等PC信息
  10. XML学习总结(二)——XML入门
  11. java构造器执行顺序一个有趣的简单实例
  12. oracle篇 之 排序、限制查询行
  13. web前端页面设计小笔记
  14. 编写Python脚本进行ARP欺骗
  15. common lisp里的几个操作符
  16. 安装VMTools工具
  17. /Date(1512551901709+0800)/转换
  18. 注意for循环中变量的作用域
  19. php学习笔记-do while循环
  20. cf 487E Tourist

热门文章

  1. CentOS Ubantu linux中实用系统相关常用命令
  2. Python档案袋( 面向对象 )
  3. Spring Boot @ControllerAdvice 处理全局异常,返回固定格式Json
  4. [Abp 源码分析]十六、后台作业与后台工作者
  5. 【java设计模式】(6)---迭代器模式(案例解析)
  6. Python爬虫入门教程 7-100 蜂鸟网图片爬取之二
  7. mac用户丢失管理员身份急救
  8. 从锅炉工到AI专家(1)
  9. PE知识复习之PE的重定位表
  10. 线程组ThreadGroup分析详解 多线程中篇(三)