多态
向上转型,动态绑定
class Shape{
public void draw(){
System.out.println("Shape::draw()");
}
}
class Cycle extends Shape{
@Override
public void draw() {
System.out.println("画圆");
}
}
class Rect extends Shape{
@Override
public void draw() {
System.out.println("画矩形");
}
}
代码块的执行顺序测试
class Animal{
protected String name;
protected int age;
static{
System.out.println("Animal::static{}");
}
{
System.out.println("Animal::instance{}");
}
public Animal(String name,int age){
this.name = name;
this.age = age;
System.out.println("Animal(String name,int age)");
}
void eat(){
System.out.println("Animal::eat()");
}
}
class Cat extends Animal{
public String sex;
public Cat(String name, int age,String sex) {
super(name, age);
this.sex = sex;
System.out.println("Cat(String name, int age,String sex)");
}
public void show(){
System.out.println(this.name + " " + this.age);
}
public void eat(){
System.out.println(this.name + "Cat::eat() ");
}
}
public class TestDemo2 {
public static void func1(){
System.out.println("func1()::static{}");
}
public static void main(String[] args) {
Animal animal = new Cat("琼琼", 18, "girl");
animal.eat();
func1();
}
}
抽象类
abstract class Shape1{
public abstract void draw();
}
abstract class A extends Shape1{
}
class B extends A{
@Override
public void draw() {
}
}
class Cycle1 extends Shape1{
@Override
public void draw() {
System.out.println("画圆");
}
}
class Rect1 extends Shape1{
@Override
public void draw() {
System.out.println("画矩形");
}
}
public class TestDemo3 {
public static void draw(Shape1 shape1){
shape1.draw();
}
public static void main(String[] args) {
Shape1 shape1 = new Cycle1();
Shape1 shape2 = new Rect1();
draw(shape1);
draw(shape2);
}
}
接口
测试1
interface IShape{
void draw();
}
class Cycle2 implements IShape{
@Override
public void draw() {
System.out.println("画圆");
}
}
class Rect2 implements IShape{
@Override
public void draw() {
System.out.println("画矩形");
}
}
public class TestDemo4 {
public static void main(String[] args) {
IShape shape = new Cycle2();
IShape shape1 = new Rect2();
shape.draw();
shape1.draw();
}
}
测试2
interface IFlying{
void run();
}
interface IRunning{
void run();
}
interface ISwimming{
public abstract void swim();
}
class Animal2{
protected String name;
public Animal2(String name){
this.name = name;
}
}
class Cat2 extends Animal2 implements IRunning{
public Cat2(String name) {
super(name);
}
@Override
public void run() {
System.out.println("猫跑");
}
}
class Dog extends Animal2 implements IRunning,ISwimming{
public Dog(String name) {
super(name);
}
@Override
public void swim() {
System.out.println("狗游泳");
}
@Override
public void run() {
System.out.println("狗跑");
}
}
abstract class AA implements IRunning{
}
class BB extends AA{
@Override
public void run() {
}
}
public class TestDemo5 {
public static void fun1(IRunning iRunning){
iRunning.run();
}
public static void main(String[] args) {
Cat2 cat = new Cat2("q琼琼");
fun1(cat);
Dog dog = new Dog("琼宝");
fun1(dog);
}
}
测试3
interface A1{
void func0();
}
interface B1{
void func1();
}
interface C1 extends A1,B1{
void func2();
}
class D implements C1{
@Override
public void func2() {
}
@Override
public void func1() {
}
@Override
public void func0() {
}
}
public class TestDemo6 {
public static void main(String[] args) {
}
}
排序打印类的信息
import java.util.Arrays;
class Student implements Comparable<Student>{
private String name;
private int age;
private double score;
public Student(String name, int age, double score){
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
@Override
public int compareTo(Student o) {
return (int)(this.score - o.score);
}
}
public class TestDemo7 {
public static void main(String[] args) {
Student[] students = new Student[2];
students[0] = new Student("baby",16,66);
students[1] = new Student("honey",18,99);
System.out.println(Arrays.toString(students));
sort(students);
System.out.println(Arrays.toString(students));
}
public static void main1(String[] args) {
int[] array = {10,4,1,9,8};
System.out.println(Arrays.toString(array));
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(Comparable[] array) {
for (int bound = 0; bound < array.length; bound++) {
for (int cur = array.length - 1; cur > bound; cur--) {
if (array[cur - 1].compareTo(array[cur]) > 0) {
Comparable tmp = array[cur - 1];
array[cur - 1] = array[cur];
array[cur] = tmp;
}
}
}
}
}
类的clone();
class Money implements Cloneable{
double money = 50.0;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
public String name;
public int age;
Money m;
public Person(){
}
public Person(String name, int age){
this.name = name;
this.age = age;
this.m = new Money();
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", m=" + m +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
Person p = (Person)super.clone();
p.m = (Money) this.m.clone();
return p;
}
}
public class TestDemo8 {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person("baby",18);
Person person1 = (Person) person.clone();
System.out.println(person.m.money);
System.out.println(person1.m.money);
System.out.println(person);
System.out.println(person1);
System.out.println("通过person2改变money");
person1.m.money = 100.0;
System.out.println(person.m.money);
System.out.println(person1.m.money);
System.out.println(person);
System.out.println(person1);
}
}