final关键字; (修饰符)
final关键字的用法:
public class FinalTest { public static void main(String[] args) { // TODO Auto-generated method stub Circle c=new Circle(4.0); c.getArea(); //c=new Circle(); } } class Circle { double r; final double pi=3.14; public Circle() {} public Circle (double r) { this.r=r; } public void getArea() { System.out.println("圆的面积是: "+r*r*pi); } }
调用方法时,传递的形式参数只是变量存储的值,没有把变量本身传过去。
不同方法 上面的局部变量是相互独立的,没有任何的关系。
比如:
public class FinalTest { public static void main(String[] args) { // TODO Auto-generated method stub final Circle c=new Circle(4.0); test(c); //c=new Circle(); } public static void test(Circle c) { c=new Circle(); c.getArea(); } }
其中的test函数中的形式参数只是名字与上面的相同,其实没有关系,也可以将形式参数名称改为 Circle b,这样不会混淆。
public class FinalTest { public static void main(String[] args) { // TODO Auto-generated method stub final Circle c=new Circle(4.0); test(c); //c=new Circle(); } public static void test(Circle b) { b=new Circle(5.0); b.getArea(); } }
结果为 圆的面积是: 78.5 而不是 50.24
常量的修饰符一般为:public static final
抽象类:
抽象类要注意的细节:
public class Abstract { public static void main(String[] args) { // TODO Auto-generated method stub Dog d=new Dog("哈士奇","白色"); d.run(); Fish f=new Fish("金鱼","红色"); f.run(); } } abstract class Animal { String name; String color; public Animal(String name,String color) { this.name=name; this.color=color; } public abstract void run(); } class Dog extends Animal{ public Dog(String name,String color) { super(name,color); } public void run() { System.out.println(name+"四条腿跑得快"); } } class Fish extends Animal{ public Fish(String name,String color) { super(name,color); } public void run() { System.out.println(name+"摇尾巴游得快"); } }
抽象类的应用场景:
抽象类的好处:强制要求子类一定要实现指定的方法。
常量的命名规范:全部字母大写,单词与单词之间使用下划线分分隔。
public class AbstractTest { public static void main(String[] args) { // TODO Auto-generated method stub Circle c=new Circle("圆形",4.0); c.getArea(); c.getLength(); Rect r=new Rect("矩形",3,4); r.getArea(); r.getLength(); } } abstract class MyShape{ String name; public MyShape() {} public MyShape(String name) { this.name=name; } public abstract void getArea() ; public abstract void getLength(); } //圆形 class Circle extends MyShape{ double r; public static final double PI=3.14; public Circle(String name,double r) { super(name); this.r=r; } public void getArea() { System.out.println(name+"的面积是:"+r*r*PI); } public void getLength() { System.out.println(name+"的周长是:"+2*PI*r); } } //矩形 class Rect extends MyShape{ int width; int height; public Rect(String name,int width,int height){ super(name); this.width=width; this.height=height; } public void getArea() { System.out.println(name+"的面积是:"+width*height); } public void getLength() { System.out.println(name+"的周长是:"+2*(width+height)); } }
abstract不能与以下关键字共同修饰一个方法:
值交换:
值传递:调用一个方法的时候,传递给方法的参数,实际上传递变量所存储的值
重点:
如果是不同的引用类型变量操作同一个对象,那么肯定回影响到结果。
一个类最多只能由一个直接的父类。 Java是单继承的
接口:
接口的定义格式:
interface 接口名{
}
接口要注意的事项:
实现接口的格式:
class 类名 implements 接口名{
}
public class Interface { public static void main(String[] args) { // TODO Auto-generated method stub PencilWithEraser p=new PencilWithEraser("2B铅笔"); p.write(); p.remove(); } } //普通的铅笔类 class Pencil{ String name; public Pencil() {} public Pencil(String name) { this.name=name; } public void write() { System.out.println(name+"沙沙的写"); } } //橡皮接口 interface Eraser{ public void remove(); } //带橡皮的铅笔 class PencilWithEraser extends Pencil implements Eraser{ public PencilWithEraser(String name) { super(name); } public void remove() { System.out.println(name+"涂改"); } }
接口的作用:
例:
public class InterfaceTest { public static void main(String[] args) { // TODO Auto-generated method stub Student s=new Student("李四"); s.study(); MoneyStudent m=new MoneyStudent("张三"); m.study(); m.makeMoney(); } } //普通的学生类 class Student{ String name; public Student() {} public Student(String name) { this.name=name; } public void study() { System.out.println(name+"好好学习"); } } //接口 会挣钱是学生的拓展功能---定义在接口上 interface Money{ public void makeMoney(); } //会挣钱的学生 class MoneyStudent extends Student implements Money{ public MoneyStudent(String name) { super(name); } public void makeMoney() { System.out.println(name+"好好挣钱,然后交学费!"); } }
类与接口之间的关系:实现关系
类与接口要注意的事项:
接口与接口的关系:继承关系
接口与接口之间的注意事项:
多态:一个对象具备多种形态(父类的引用类型变量指向了子类的对象或者是接口的引用类型变量指向了接口实现类的对象) 动物 a=new 狗();
多态的前提:必须存在继承或者实现关系
多态要注意的细节:
总结:
多态情况下,子父类存在同名的成员时,访问的都是父类的成员,除了在同名的非静态函数时,访问的才是子类的
编译看左边,运行不一定看右边
编译看左边:java编译器在编译的时候,会检查引用类型变量所属的类是否具备指定的成员,如果不具备马上报错
多态的应用:
1.多态用于形式参数类型的时候,可以接受更多类型的数据
2.多态用于返回值类型的时候,可以返回更多类型的数据
public class Kinds { public static void main(String[] args) { // TODO Auto-generated method stub Circle c=new Circle("圆形",4.0); print(c); Rect r=new Rect("矩形",3,4); print(r); } public static void print(MyShape s) { //MyShape s=new Circle(); 多态 s.getArea(); s.getLength(); } } abstract class MyShape{ String name; public MyShape() {} public MyShape(String name) { this.name=name; } public abstract void getArea() ; public abstract void getLength(); } //圆形 class Circle extends MyShape{ double r; public static final double PI=3.14; public Circle(String name,double r) { super(name); this.r=r; } public void getArea() { System.out.println(name+"的面积是:"+r*r*PI); } public void getLength() { System.out.println(name+"的周长是:"+2*PI*r); } } //矩形 class Rect extends MyShape{ int width; int height; public Rect(String name,int width,int height){ super(name); this.width=width; this.height=height; } public void getArea() { System.out.println(name+"的面积是:"+width*height); } public void getLength() { System.out.println(name+"的周长是:"+2*(width+height)); } }
结果为:
圆形的面积是:50.24
圆形的周长是:25.12
矩形的面积是:12
矩形的周长是:14
多态的好处:提高了代码的拓展性
应用二:
public static void main(String[] args) { // TODO Auto-generated method stub Circle c=new Circle("圆形",4.0); print(c); Rect r=new Rect("矩形",3,4); print(r); MyShape m=getShape(1); //注意不能使用Rect或者Circle类型来接受,因为函数返回值类型为MyShape不能确定到底返回的是哪种 m.getArea(); m.getLength(); MyShape n=getShape(0); //调用了使用多态的方法,定义的变量类型 n.getArea(); n.getLength(); } //定义一个函数接受任意类型的图形,然后计算任意图形的周长及面积 public static void print(MyShape s) { //MyShape s=new Circle(); 多态 s.getArea(); s.getLength(); } //定义一个函数返回任意类型的图形 public static MyShape getShape(int i) { if (i==0) { return new Circle("圆形",4.0); } else { return new Rect("矩形",3,4); } }
此时结果为:
矩形的面积是:12
矩形的周长是:14
圆形的面积是:50.24
圆形的周长是:25.12
原文:https://www.cnblogs.com/zhangwugai/p/10322943.html