面对对象
发布日期:2021-05-10 09:45:12 浏览次数:18 分类:精选文章

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

Java������������������������

������������������������

1.1 ������������������������

���Java��������������������������������������������������������������������������������������������� bundling ������������������������������������������������

public class Person {    // ���������    String name;    int age;    boolean isMale;        // ���������    public void eat() {        System.out.println("���������������");    }        public void sleep() {        System.out.println("���������������");    }        public void talk(String language) {        System.out.println("���������������������������" + language);    }}

���������������������

Person p1 = new Person();p1.name = "Tom";p1.isMale = true;p1.eat(); // ������������

1.2 ������������������

���������������������������������������������������������������������������������������������������������

Person p2 = new Person();// p2.name ������ null��������������������� setXX ���������������������������������������

���������������������������

Person p1 = new Person();Person p3 = p1;p3.age = 10; // ������ p3 ��� age���p1 ��� age ���������������

������������������������

2.1 ������������������������������

  • ������������

    • ���������������dataType variableName = value;
    • ������������������
    • ���������������������
  • ������������

  • ���������������

    • ������������������ {} ���������
    • ���������������������������������������������������������
  • ������������������

    • ������������������ public���private���protected ������
    • ������������������������������������
  • ���������������������

    • ������������������������������������������������������������ false��������������� null���
    • ���������������������������
  • ���������������

    • ������������������
    • ������������������������

������������������������

  • ���������������
  • ������������
  • ������������������
  • ������������������
  • ������������������������������


    ���������������������������������

    3.1 ������������������ ����'n'

    ������������������������������������������������������������������������������������������������������������������

    ���������������������

    ��������������� ��������������� ���������(������������) {    ���������}
    • ������������������public���private���protected���������
    • ���������������������������������������������
      • ��������������������� return ���������
      • ������������������������������void

    ���������������overloading������

    public class Customer {    // ������    String name;    int age;    boolean isMale;    // ������    public void eat() {        System.out.println("������������");    }    public void sleep(int hours) {        System.out.println("������������" + hours + "������");    }    public String getName() {        return name;    }    public String getNation(String nation) {        return "������������������" + nation;    }}

    ���������������������

    ���Java���������������������������������������������������������������������������������������������

    4.1 null ���������������

    ���������������������������������������������null ������������������

    4.2 ������������

    ���������������������������������������������������������������������������

    new Phone().sendEmail();Phone p = new Phone();p.sendEmail();

    ������������������������

    ������������������������������������������������������������������������������������

    public class MethodArgsTest {    public static void main(String[] args) {        MethodArgsTest test = new MethodArgsTest();        test.show(12); // ������������������������        test.show("hello");        test.show("hello", "world");        test.show();                test.show(new String[]{"AA", "BB", "CC"});    }    public void show(int i) {}    public void show(String s) {        System.out.println("show(String)");    }        public void show(String... strs) {        System.out.println("show(String...)");        for (String s : strs) {            System.out.println(s);        }    }}

    ���������������������

    ���������������������������������������������������������

    public class OverLoadTest {    public static void main(String[] args) {        OverLoadTest test = new OverLoadTest();        test.getSum(1, 2);    }    void getSum(int i, int j) {        System.out.println("1");    }        void getSum(double d1, double d2) {        System.out.println("2");    }        void getSum(String s, int i) {        System.out.println("3");    }        void getSum(int i, String s) {        System.out.println("4");    }}

    ���������������������

    ������������������������������������������������������������

    • ������������������������������������������������
    • ���������������������������������������
    public class ValueTransferTest {    public static void main(String[] args) {        Order o1 = new Order();        o1.orderId = 1001;                Order o2 = o1;        System.out.println("o1.orderId = " + o1.orderId);        System.out.println("o2.orderId = " + o2.orderId);                o2.orderId = 1002;        System.out.println("o2.orderId = " + o2.orderId);    }}class Order {    int orderId;}

    ������������������������������

    ������������������������������������������������������

    public class ValueTransferTest1 {    public static void main(String[] args) {        int m = 10;        int n = 20;                // ���������������        int temp = m;        m = n;        n = temp;                ValueTransferTest1 test = new ValueTransferTest1();        test.swap(m, n);        System.out.println(m + ", " + n);    }    void swap(int m, int n) {        int temp = m;        m = n;        n = temp;    }}

    ���������������������������

    1. ���������������

    ���������������������������������������������������������������������������������������������������

    2. ������������

    ���������������������������������������������������������������������������������������

    3. ���antuolu

    ���������������������������������������������������������������


    ���������������

    10.1 ���������������������������

    ���������������������������������������������������������������������

    public class Person {    String name;    int age;        public Person(String name, int age) {        name = name; // ������������������������������        this.age = age;    }}

    10.2 ���������������

    ���������������������������������������������������������������������

    public class Box {    int length;    int width;    int height;    Box() {        length = 0;        width = 0;        height = 0;    }    Box(int l, int w) {        length = l;        width = w;        height = 0;    }    Box(int l, int w, int h) {        length = l;        width = w;        height = h;    }}

    ���������������������������������������������������������������������������������������������������������������

    上一篇:关于Eclipse 的 Debug 中 Step into进不去方法的问题解决
    下一篇:Eclipse的基本使用配置

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月22日 16时24分54秒