多态性的理解
package com.spring.item3;
public class GeometricTest {
public static void main(String[] args) {
GeometricTest test = new GeometricTest();
test.displayArea(new Circle("sdhk", 1, 2));
test.equalsArea(new Circle("baidh", 1, 2), new Circle("jdh", 1, 1));
System.out.println(test.equalsArea(new Circle("baidh", 1, 2), new Circle("jdh", 1, 1)));
}
public boolean equalsArea(GeometricObject g1, GeometricObject g2) {
System.out.println(g1.findArea());
System.out.println(g2.findArea());
return g1.findArea() == g2.findArea();
}
public void displayArea(GeometricObject g) {
System.out.println("的面积为:" + g.findArea());
}
}
“==”和equals
1.回顾==的使用
1.1 ==:运算符
1.2 可以使用在基本数据类型变量和引用数据类型的变量中;
1.3 基本数据变量:比较两个变量保存的数据是否相等(不一定类型相同boolean
类型除外)
引用数据变量:比较两个对象的地址值是否相同;
2.equals()方法的使用
2.1 equals()是一个方法,而不是运算符;
2.2 只能适用于引用数据类型;
2.3 Object类中equals()的定义和“==”一样;比较的为地址值;
2.4 像String,Data,File,包装类等都重写了equals()方法,
比较的是两个对象的实体内容是否相同;
public class EqualsTest {
public static void main(String[] args) {
int i = 10;
int j = 10;
double d = 10.0;
System.out.println(i == j);
System.out.println(i == d);
boolean b = true;
char c = 10;
System.out.println(i == c);
char c1 = 'A';
char c2 = 65;
System.out.println(c1 == c2);
Customer cust1 = new Customer("Tom",21);
Customer cust2 = new Customer("Tom",21);
System.out.println(cust1 == cust2);
String str1 = new String("atguigu");
String str2 = new String("atguigu");
System.out.println(str1 == str2);
System.out.println("****************************");
System.out.println(cust1.equals(cust2));
System.out.println(str1.equals(str2));
Date date1 = new Date(32432525324L);
Date date2 = new Date(32432525324L);
System.out.println(date1.equals(date2));
}
}
toString
toString()方法在Object类中定义,其返回值是String类型,返回类名和它
的引用地址。
在进行String与其它类型数据的连接操作时,自动调用toString()方法
Date now=new Date();
System.out.println(“now=”+now); 相当于
System.out.println(“now=”+now.toString());
可以根据需要在用户自定义类型中重写toString()方法
如String 类重写了toString()方法,返回字符串的值。
s1=“hello”;
System.out.println(s1);
基本类型数据转换为String类型时,调用了对应包装类的toString()方法
int a=10; System.out.println(“a=”+a);
public class ToStringTest {
public static void main(String[] args) {
Customer cust1 = new Customer("Tom",21);
System.out.println(cust1.toString());
System.out.println(cust1);
String str = new String("MM");
System.out.println(str);
Date date = new Date(4534534534543L);
System.out.println(date.toString());
}
}
this和super
![](https://i-blog.csdnimg.cn/blog_migrate/f7e65358c4495a9cad78c9d185d5905e.png)
重写equals
![](https://i-blog.csdnimg.cn/blog_migrate/20b1b461cd38d23d33a3debd9e743d54.png)
包装类
![](https://i-blog.csdnimg.cn/blog_migrate/ecc58b50ba31db3423d7f05b80786256.png)
package com.spring.demo1;
import org.junit.Test;
public class WrapperTest {
@Test
public void Test1() {
byte num0 = 10;
int num1 = 10;
short num2 = 10;
long num3 = 10L;
float num4 = 10.0F;
double num5 = 10.0;
boolean num6 = false;
char num7 = 'A';
Byte b = num0;
System.out.println(b);
Integer i = num1;
System.out.println(i);
Short s = num2;
System.out.println(s);
Long l = num3;
System.out.println(l);
Double d = num5;
System.out.println(d);
Boolean B = num6;
System.out.println(B);
Character character = num7;
System.out.println(character);
}
@Test
public void Test2() {
}
@Test
public void Test3() {
int num1 = 10;
String str1 = num1 + "";
float f1 = 12.3f;
String str2 = String.valueOf(f1);
double d2 = 12.6;
System.out.println(String.valueOf(d2));
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2);
System.out.println(str3);
}
@Test
public void Test4() {
String str1 = "123";
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);
System.out.println(Float.parseFloat(str1));
System.out.println(Double.parseDouble(str1));
System.out.println(Boolean.parseBoolean(str1));
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
}
static关键字的使用
1. static使用范围:属性,方法,代码块,内部类;
2. 使用static修饰属性:静态变量(类变量)
2.1 属性:按是否使用static修饰,分为:静态属性vs非静态属性
静态变量:我们创建类的多个对象后,多个对象可以共享一个静态变量,
当通过某一个对象来修改静态变量时,会导致其他属性调用此变量时时
修改过的;(可以通过“类.静态属性”进行调用赋值。)
2.2 实例变量(非static属性):我们创建类的多个对象,每一个对象都独
立的拥有一套类中的非static属性,当某一个对象修改其中的非static
属性时,不会导致其他对象中同样的属性值修改;
3. static修饰属性的补充说明
3.1 static变量会随着类的加载而加载,可以通过“类.static变量”进行调用
3.2 static变量的加载要早于对象的创建;
3.3 由于类只加载一次,则static变量在内存中也只会存在一份:存在方法区
的静态域中
4. 使用static修饰方法:静态方法
4.1 随着类的加载而加载,通过“类。static方法”进行调用;
4.2 static方法中,只能调用静态的方法或属性;
public static void eat(){ }
4.3 非static方法中,既可以调用非static的方法和属性,也可以调用static
的方法和属性;
4.4 在static方法内不可以使用this,super关键字;(因为这两个关键字是
有对象之后使用的);
5. 开发中如何确定一个属性和方法是否要声明为static?
5.1 属性可以被多个对象共享,不会随着对象的不同而不同(简称你有的我也
会有)
5.2 操作static属性的方法,通常设置为static,以及各种工具类,方便
“类。static方法”的调用;
public class StaticTest{
public static void main(String[] args){
china.nation="中国";
China.show();
chian c=new chian();
c.nation="chinese";
}
}
class China{
int age;
String name;
static String nation;
public void eat(){
System.out.println("中国人吃中餐");
this.info();
System.out.println("name :" +name);
walk();
System.out.println("nation : " + nation);
}
public static void show(){
System.out.println("我是一个中国人!");
System.out.println(Chinese.nation);
walk();
}
public void info(){
System.out.println("name :" + name +",age : " + age);
}
public static void walk(){
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6eb246fa87890f96a6127407f8542071.png)
static使用情景
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle(2.3);
Circle c2 = new Circle(2.6);
Circle c3 = new Circle(2.36);
Circle c4 = new Circle(2.77);
System.out.println(c1.getRadius());
System.out.println(c1.getId());
System.out.println(Circle.getTotal());
}
}
class Circle {
private double radius;
private int id;
private static int total;
private static int intid = 1001;
public Circle(double radius) {
this.id = intid++;
total++;
this.radius = radius;
}
单例设计模式
1.单例设计模式:
class SingleCase{
1.1 构造器私有化;
1.2 在该类的内部创建类的对象,且要设置为static;
1.3 创建一个公共的static的方法,供外界调用,用来返回该类的对象
}
package com.spring.demo1;
public class SingleCaseTest {
public static void main(String[] args) {
SingleCase case1 = SingleCase.getInstance();
SingleCase case2 = SingleCase.getInstance();
System.out.println(case1==case2);
System.out.println(case2);
}
}
class SingleCase{
private SingleCase(){
}
private static SingleCase single=new SingleCase();
public static SingleCase getInstance(){
return single;
}
}
类的成员之四:代码块
1. 代码块的作用:用来初始化对象,类;
2. 只能使用static修饰;
3. 内部可以有输出语句;
4. static代码块随着类的加载而执行,且只执行一次;
5. 非static代码块随着对象的创建而执行,每创建一个对象就执行一次;
6. static代码块内只能调用static的属性,方法;
7. 非static代码块可以调用static或者非static的属性和方法;
对属性可以赋值的位置:
①默认初始化
②显式初始化/⑤在代码块中赋值
③构造器中初始化
④有了对象以后,可以通过"对象.属性"或"对象.方法"的方式,进行赋值
执行的先后顺序:① - ② / ⑤ - ③ - ④
{
System.out.println("hello, block - 2");
}
{
System.out.println("hello, block - 1");
age = 1;
eat();
desc = "我是一个爱学习的人1";
info();
}
static{
System.out.println("hello,static block-2");
}
static{
System.out.println("hello,static block-1");
desc = "我是一个爱学习的人";
info();
}
final关键字
1. final可以用来修饰:类,方法,变量;
2. 用来修饰一个类,则此类不能被继承,如String System类;
3. 用来修饰方法,则次方法不能被重写;
4. 用来修饰变量,此时的变量为一个常量;
4.1 final修饰属性(必须初始化);显示初始化,代码块初始化,构造器;
4.2 final修饰局部变量,赋给形参之后,不能对形参重新赋值;
5. static final 用来修饰属性:全局常量。
6. static可以用来修饰:方法,属性,代码块,内部类;
public class FinalTest {
final int WIDTH = 0;
final int LEFT;
final int RIGHT;
{
LEFT = 1;
}
public FinalTest(){
RIGHT = 2;
}
public FinalTest(int n){
RIGHT = n;
}
abstract关键字
1. abstract可以用来修饰:类,方法;
2. abstract修饰类:抽象类;
2.1 此类不能被实例化(不能new对象);
2.2 抽象类中一定有构造器,便于子类实例化使调用;
2.3 开发中,都会提供抽象类的子类,让子类对象实例化,完成相关操作;
3. abstract修饰方法:抽象方法
3.1 抽象方法只有方法的声明,没有方法体;
public abstract void eat();
3.2 有抽象方法的类一定是抽象类,反之,抽象类中可以没有抽象方法;
3.3 若子类重写父类中所有抽象方法(包括直接父类和间接父类),此子类才可
实例化(new对象);
若子类没有重写父类中所有的抽象方法(包括直接父类和间接父类),则此
子类也是一个abstract类;
4. abstract不能用来修饰属性,构造器,不能用来修饰private方法,static方法,
final方法和类(不能重写不能继承);
public class AbstractTest {
public static void main(String[] args) {
}
}
abstract class Creature {
public abstract void breath();
}
abstract class Person extends Creature {
String name;
int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public abstract void eat();
public void walk() {
System.out.println("人走路");
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/3ad42cf5b805d5fbee8f3bdae13f5cf8.png)
interface关键字(接口)
1. java中,类(class)和接口(interface)是并列的两个结构;
2. 接口定义(interface):
interface 接口名{ };
class 类名{ };
3.interface 接口名{
>全局常量:public static final int MAX;
>抽象方法:public abstract void getMax();
}
3. 接口不能定义构造器,意味着接口(interface)不能实例化;(不能new)
4. 类----接口(implements)实现
4.1 如果类覆盖了接口(interface)中所有的抽象方法,则可以new;
4.2 如果类没有覆盖接口(interface)中所有抽象方法(public abstract)
则不能new;
4.3 类----类(extends)继承;
5.java类可以实现多个接口----弥补了java单继承的局限性
5.1 class A extends B implements C,D,E{ }
6. 接口与接口直接可以继承,而且可以多继承
6.1 接口----接口(extends)继承
package com.spring.test;
public class InterfaceTest {
public static void main(String[] args) {
Plane p1 = new Plane();
p1.fly();
}
}
interface Flyable {
public static final int MAX = 111;
int MIN = 2;
public abstract void fly();
void stop();
}
interface Appearance {
void wire();
}
class Plane implements Flyable {
@Override
public void fly() {
System.out.println("通过引擎起飞");
}
@Override
public void stop() {
System.out.println("驾驶员减速停止");
}
}
abstract class Kites extends Object implements Flyable, Appearance {
@Override
public void wire() {
}
@Override
public void fly() {
}
}
接口(interface)新特性
![](https://i-blog.csdnimg.cn/blog_migrate/f40a33b76c22b0075f50554106f9235c.png)
![2s8tRU.png](https://i-blog.csdnimg.cn/blog_migrate/ddec21900e5257e1f32731e96fcc3638.png)
类的成员五:内部类
1. java中允许将一个类A声明在另一个 类B中,则A就是内部类,B为外部类;
2. 作为外部类成员(参考属性)
2.1 可以调用外部类的结构
2.2 可以被static修饰
2.3 可以被4种不同的权限修饰
3 作为一个类
3.1 类内可以定义方法,属性,构造器
3.2 可以被final修饰,表示不能被继承,没有final则可以继承
3.3 可以被abstract修饰
public class InnerClassTest {
public static void main(String[] args) {
Person.Dog dog = new Person.Dog();
dog.show();
Person p = new Person();
Person.Bird bird = p.new Bird();
bird.sing();
System.out.println();
bird.display("黄鹂");
}
}
class Person{
String name = "小明";
int age;
public void eat(){
System.out.println("人:吃饭");
}
static class Dog{
String name;
int age;
public void show(){
System.out.println("卡拉是条狗");
}
}
class Bird{
String name = "杜鹃";
public Bird(){
}
public void sing(){
System.out.println("我是一只小小鸟");
Person.this.eat();
eat();
System.out.println(age);
}
public void display(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(Person.this.name);
}
}