zoukankan      html  css  js  c++  java
  • java 类的继承

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    class Another {
        String name;
    }
    
    public class testcnm extends Another {
        int salary;
    
        public static void main(String[] args){
            testcnm obj = new testcnm();
            obj.salary = 10000;
            obj.name = "Prakash";
            System.out.println("Name: " + obj.name);
            System.out.println("Salary: " + obj.salary);
        }
    
    }

     2

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    
    class Salary extends Details {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    public class testcnm {
        public static void main(String[] args) {
            Salary sal = new Salary();
            sal.detailMethod();
            sal.SalaryMethod();
        }
    }

     3

    package testpacknm;
    
    import java.util.Scanner;
    import testpacknm.testcnm;
    
    
    public class testcnm {
        public static void main(String[] args) {
            Salary sal = new Salary();
            sal.detailMethod();
            sal.SalaryMethod();
            sal.data();
        }
    }
    
    class Salary extends Details {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details extends Database {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    class Database {
        public void data() {
            System.out.println("Callind database method");
        }
    }

     4

    /**
     * Written for 'JavaDump' Android Application
     */
    // Hierarchical Inheritance Example
    public class Employee {
        public static void main(String[] args) {
            Salary sal = new Salary();
            // sal.detailMethod(); //throws error
            sal.SalaryMethod();
            sal.data();
        }
    }
    
    class Salary extends Database {
        public void SalaryMethod() {
            System.out.println("Calling the salary method");
        }
    }
    
    class Details extends Database {
        public void detailMethod() {
            System.out.println("Calling detail method");
        }
    }
    
    class Database {
        public void data() {
            System.out.println("Callind database method");
        }
    }
    
    
    OUTPUT:
    Calling the salary method
    Callind database method
  • 相关阅读:
    终端操作各插件安装配置
    浅谈CSRF攻击方式
    教你突破基于HTTP_REFERER的防盗链的方法
    HTTP Referer简介
    如何利用百度蜘蛛referer找到报错页面入口?
    Flex 布局教程:语法
    HTTP 状态码
    RESTful API 设计最佳实践
    Javascript原型和原型链
    JS判断是什么设备是什么浏览器-主要用移动页面开发
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12076029.html
Copyright © 2011-2022 走看看