Java例题

使用super调用父类的构造方法

  1. class Student{
  2. int number;
  3. String name;
  4. Student(){
  5. }
  6. Student(int number,String name){
  7. this.number=number;
  8. this.name=name;
  9. System.out.println("我的名字是:"+name+"学号是:"+number);
  10. }
  11. }
  12. class UniverStudent extends Student{
  13. boolean 婚否;
  14. UniverStudent(int number,String name,boolean b){
  15. super(number,name);
  16. 婚否=b;
  17. System.out.println("婚否="+婚否);
  18. }
  19. }
  20. public class exam {
  21. public static void main(String[] args) {
  22. UniverStudent zhang =new UniverStudent(9901,"何晓林",false);
  23. }
  24. }


final关键字

  1. class A{
  2. final double PI=3.1415926; //PI常量
  3. public double getArea(final double r){
  4. //r=r+1; 非法,不允许对final变量进行更新操作
  5. return PI*r*r;
  6. }
  7. public final void speak(){
  8. System.out.println("您好,How' everything here?");
  9. }
  10. }
  11. public class exam {
  12. public static void main(String[] args) {
  13. A a=new A();
  14. System.out.println("面积:"+a.getArea(100));
  15. a.speak();
  16. }
  17. }

对象的上转型对象

  1. class 类人猿{
  2. void crySpeak(String s){
  3. System.out.println(s);
  4. }
  5. }
  6. class People extends 类人猿{
  7. void computer(int a,int b){
  8. int c=a*b;
  9. System.out.println(c);
  10. }
  11. void crySpeak(String s){
  12. System.out.println("***"+s+"***");
  13. }
  14. }
  15. public class exam {
  16. public static void main(String[] args) {
  17. 类人猿 monkey;
  18. People geng = new People();
  19. monkey =geng;//monkey是People对象geng的上转型对象
  20. monkey.crySpeak("I love this game");
  21. //同等于geng.crySpeak("I love this game");
  22. People people=(People)monkey; //把上转型对象强置转化为子类的对象
  23. people.computer(10, 10);
  24. }
  25. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值