博客
关于我
封装和继承
阅读量:470 次
发布时间:2019-03-06

本文共 5974 字,大约阅读时间需要 19 分钟。

Java编程基础入门知识

方法的参数传递与返回值

Java语言在方法调用时采用传值的方式进行参数传递。具体来说:

  • 基本数据类型传递的是数据的值本身。
  • 引用数据类型传递的是对象的地址(即引用),而非对象本身。

基本数据类型传递

public class PassValue {    public void change(int x) {        x = 100;        System.out.println("方法中x==" + x);    }}public class Test {    public static void main(String[] args) {        PassValue pv = new PassValue();        int x = 5;        System.out.println("方法调用之前x==" + x);        pv.change(x);        System.out.println("方法调用之后x==" + x);    }}

引用数据类型传递

public class PassValue2 {    int x;    public void change(PassValue2 obj) {        obj.x = 100;        System.out.println("方法中obj.x==" + obj.x);    }}public class Test {    public static void main(String[] args) {        PassValue2 p = new PassValue2();        p.x = 5;        System.out.println("方法调用之前p.x==" + p.x);        p.change(p);        System.out.println("方法调用之后p.x==" + p.x);    }}

返回值类型

  • 基本数据类型作为返回值时,返回的是实际数据。
  • 引用数据类型作为返回值时,返回的是对象的地址。

封装性

Java具有三大特征:封装性、继承性和多态性。封装性的主要作用是隐藏对象的实现细节。

封装的实现

为了实现封装性:

  • 将不想被类外直接访问的成员变量私有化。
  • 为私有成员提供公共方法进行访问,通常通过getset方法。
  • 示例

    public class Person {    private String name;    private int age;    private String sex;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        if (age > 100 || age < 1) {            this.age = 18;        } else {            this.age = age;        }    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        if (sex.equals("男") || sex.equals("女")) {            this.sex = sex;        } else {            this.sex = "男";        }    }    public void eat() {        System.out.println(name + "开始吃饭...");    }    public void study() {        System.out.println(name + "开始努力学习...");    }    public void showInfo() {        System.out.println("姓名:" + name + " 年龄:" + age + " 性别:" + sex);    }}

    static关键字

    static关键字用于修饰类成员,包括成员变量、方法和代码块。

    静态属性

    静态属性属于类,而不是单个对象,所有对象共享同一个值。

    public class Person {    String name;    int age;    static int totalCount = 1300000000;}public class DemoPerson {    public static void main(String[] args) {        System.out.println(Person.totalCount);    }}

    静态方法

    静态方法属于类,可以通过类名直接调用。

    public class Person {    static int totalCount;    public static void calcTotalCount() {        System.out.println("统计人口方法");        totalCount = 1350000000;    }}public class DemoPerson {    public static void main(String[] args) {        Person.calcTotalCount();    }}

    静态导入

    静态导入允许导入某类的静态成员。

    import static java.lang.System.out;import static java.lang.Integer.*;public class Demo {    public static void main(String[] args) {        out.println(MAX_VALUE);        out.println(toHexString(36));    }}

    继承性

    继承的概念

    继承是指一个子类在继承基类的基础上,增添新的功能或属性。

    继承的好处

    • 代码复用:避免重复代码。
    • 模拟现实世界:如继承关系反映生物学上的关系。

    继承语法

    public class FatherClass {}public class ChildClass extends FatherClass {}

    示例

    public class Person {    String name;    int age;    String birthday;    public void showInfo() {        System.out.println("姓名:" + name + " 年龄:" + age + " 出生日期:" + birthday);    }}public class Student extends Person {    String school;    public void study() {        System.out.println(name + "好好学习天天向上");    }}

    方法重写

    方法重写的概念

    方法重写是指在继承过程中,子类对父类方法进行完善或重新实现。

    方法重写规则

    • 方法名参数列表返回值必须与父类方法一致。
    • 访问权限不能比父类严格。
    • 返回值可以是引用类型。

    示例

    public class Animal {    String nickname;    String color;    String strain;    public Animal() {        System.out.println("父类Animal的构造方法执行了..........");    }    public Animal(String nickname, String color, String strain) {        this.nickname = nickname;        this.color = color;        this.strain = strain;    }    public void print() {        System.out.println("本动物昵称:" + nickname + " 颜色:" + color + " 品种:" + strain);    }}public class Dog extends Animal {    int love;    public Dog() {        super();        System.out.println("Dog子类的构造方法执行了");    }    public Dog(String nickname, String color, String strain, int love) {        super(nickname, color, strain);        this.love = love;    }    public void lookHome() {        System.out.println(nickname + "正在给主人看家...........");    }    public String printInfo() {        System.out.println("狗狗信息:昵称:" + super.naming + " 颜色:" + super.color + " 品种:" + super.strain + " 亲密度:" + this.love);        return "haha";    }}

    包与访问权限

    包的作用

    包用于管理类和接口,避免命名冲突。

    包命名规范

    • 通常使用公司域名的倒置加上项目名或模块名。
    • 示例:com.baidu.oacom.alibaba.pay

    访问权限

    权限级别 同类 同包子类 不同包子类 不同包类
    public
    protected
    default
    private

    示例

    package a;public class Person {    public String name;    protected int age;    char sex;    private double sal;    public Person(String name, int age, char sex, double sal) {        this.name = name;        this.age = age;        this.sex = sex;        this.sal = sal;    }    public static void main(String[] args) {        Person p = new Person("张三", 12, 'm', 5000);        System.out.println(p.name);        System.out.println(p.age);        System.out.println(p.sex);        System.out.println(p.sal);    }}

    方法重载与重写的区别

    • 方法重载:同一个类中,方法名相同,参数列表不同。
    • 方法重写:继承过程中,子类对父类方法的实现。

    示例

    public class Animal {    String nickname;    String color;    String strain;    public Animal() {        System.out.println("父类Animal的构造方法执行了..........");    }    public Animal(String nickname, String color, String strain) {        this(nickname, color, strain);    }    public void print() {        System.out.println("本动物昵称:" + nickname + " 颜色:" + color + " 品种:" + strain);    }}public class Dog extends Animal {    int love;    public Dog() {        super();        System.out.println("Dog子类的构造方法执行了");    }    public Dog(String nickname, String color, String strain, int love) {        super(nickname, color, strain);        this.love = love;    }    public void lookHome() {        System.out.println("狗狗正在给主人看家...........");    }    public String printInfo() {        System.out.println("狗狗信息:昵称:" + super.naming + " 颜色:" + super.color + " 品种:" + super.strain + " 亲密度:" + this.love);        return "haha";    }}

    转载地址:http://olwfz.baihongyu.com/

    你可能感兴趣的文章
    Node.js基于Express框架搭建一个简单的注册登录Web功能
    查看>>
    node.js学习之npm 入门 —8.《怎样创建,发布,升级你的npm,node模块》
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    Node.js安装及环境配置之Windows篇
    查看>>
    Node.js安装和入门 - 2行代码让你能够启动一个Server
    查看>>
    node.js安装方法
    查看>>
    Node.js官网无法正常访问时安装NodeJS的方法
    查看>>
    node.js模块、包
    查看>>
    node.js的express框架用法(一)
    查看>>
    Node.js的交互式解释器(REPL)
    查看>>
    Node.js的循环与异步问题
    查看>>
    Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
    查看>>
    nodejs + socket.io 同时使用http 和 https
    查看>>
    NodeJS @kubernetes/client-node连接到kubernetes集群的方法
    查看>>
    NodeJS API简介
    查看>>
    Nodejs express 获取url参数,post参数的三种方式
    查看>>
    nodejs http小爬虫
    查看>>
    nodejs libararies
    查看>>
    nodejs npm常用命令
    查看>>
    nodejs npm常用命令
    查看>>