继承,对象间的类型转换,接口,多态的代码案例,以及对自身的反思

//继承

public class Person {

public String name;
public int age;
public void say(){
System.out.println(name+" can say");
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}

}

public class student extends Person{
public String schoolname;
public void Gotoschool(){
System.out.println("I am going to school");
}
}

public class Test {
public static void main(String[] args) {
student s1=new student();
s1.name="MingM";
s1.age=10;
s1.schoolname="CQ";
s1.say();
s1.Gotoschool();
System.out.println("My name is"+s1.name);
System.out.println("My schoolname is"+s1.schoolname);


}
}

输出结果:

MingM can say

I am going to school

My name isMingM

My schoolname isCQ


继承使得代码得以优化,易于管理,是实现代码复用的最有效的方法

--------------------------------------------------------------------------------------------------------------------------------

//对象间的类型转换


public class A{
String s="class A";
}

public class B extends A{
String s="class B";
}

public class Test{
public static void main(String[] args) {
B bb,b=new B();
A a,aa;
a=(A)b;
aa=b;
System.out.println(a.s);
System.out.println(aa.s);
bb=(B)a;
System.out.println(bb.s);
}
}

输出结果:

class A
class A
class B


虽然将对象间的类型转换了,但是并没有转换类内的值,所以a.s和aa.s的类型转换完却还是A类的s的值


--------------------------------------------------------------------------------------------------------------------------------

interface A {
int a=1;
void showA();
}

interface B {
int b=2;
void showB();
}

interface C extends A,B{
int c=3;
void showC();
}

class D implements C{
public void showA(){
System.out.println("aaaa="+a+a+a+a);
}
public void showB(){
System.out.println("bbbb="+b+b+b+b);
}
public void showC(){
System.out.println("cccc="+c+c+c);
}
public static void main(String[] args) {
D d1=new D();
d1.showA();
d1.showB();
d1.showC();
}
}

输出结果:

aaaa=1111
bbbb=2222
cccc=333


接口类中的方法都是抽象的,所以跟抽象类一样无法自主实现,需要子类来实现,且子类在实现接口时必须实现接口中的方法


--------------------------------------------------------------------------------------------------------------------------------


public abstract class Shape {
public abstract double getArea();
}

public class Circle extends Shape{
private double r=0;
private final static double PI=3.14;
public Circle(double r){
this.r=r;
}
public double getArea(){
return (PI*r*r);
}
}

public class Square extends Shape{
private double height=0;
public Square(double height){
this.height=height;
}
public double getArea(){
return (this.height*this.height);
}
}

public class Triangle extends Shape{
private double a=0;//三角形的边:a
private double b=0;//三角形的边:b
private double c=0;//三角形的边:c
private double h=0;//三角形的高
public Triangle(double a,double h){
this.a=a;
this.h=h;
}
public Triangle(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
public double getArea(){
if(h==0){
double s=(a+b+c)/2;
return Math.pow(s*(s-a)*(s-b)*(s-c),0.5);
}else{
return (a*h/2);
}
}
}

public class TestShape {
public static void main(String[] args) {
Shape square=new Square(3);
Shape circle=new Circle(2);
Shape triangle1=new Triangle(3,4,5);
Shape triangle2=new Triangle(3,4);
System.out.println(square.getArea());
System.out.println(circle.getArea());
System.out.println(triangle1.getArea());
System.out.println(triangle2.getArea());
}
}


输出结果:

9.0
12.56
6.0
6.0


这是利用多态创建的图形类,通过抽象出的父类来定义一个公共的函数来实现求面积的功能,子类通过重写父类中的方法来实现求自身面积的方法 ,跟我期末考试的编程题思路是一样的



说到期末考试,我必须认真的反思下我最近的行为了,可以说是极度膨胀了,现在想想,真TM是个SB。也幸亏被我的导师骂醒,真的要谢谢他,让我能够认清现在的自己是多么的微不足道,也感谢他能让我在今后的二年不会浑浑噩噩的度过。讲道理,如果不学JAVA,或者我学JAVA的时候不是碰到他,我估计我现在不是在写博客,而是在通宵打游戏吧,虽然我醒悟的有点晚(可以说是非常的晚了),但是我认真的去学这个东西,也不至于毕业的时候一事无成,面试的 时候没有一点底气也没有。今天在学习JSP的基本概念时,接触到了许多东西:C/S结构,B/S结构,Model1工作原理和Model2工作原理,JSP,JavaBean,Servlet的作用等等,打开一扇新世界的大门。最后的最后,借用我老师的一句话,脚踏实地的去学,不要浮躁,不要膨胀,我跟那些牛逼的人比起来,屁都不是!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值