面向对象中

例子1

Person类

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:38
 */
public class Person {
    String name;
    int age;

    public Person(){

    }

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

    public void eat(){
        System.out.println("吃饭");
    }

    public void sleep(){
        System.out.println("睡觉");
    }

}

Student

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:39
 */
public class Student extends Person{

//    String name;
//    int age;
    String major;

    public Student() {
    }

    public Student(String name, int age, String major) {
        this.name = name;
        this.age = age;
        this.major = major;
    }
//    public void eat(){
//        System.out.println("吃饭");
//    }
//
//    public void sleep(){
//        System.out.println("睡觉");
//    }

    public void study(){
        System.out.println("学习");
    }


}

ExtendsTest

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:47
 */
public class ExtendsTest {
    public static void main(String[] args) {

        Person p1 = new Person();
        p1.age=1;
        p1.eat();

        Student s1 = new Student();
        s1.eat();
        s1.sleep();

        s1.name="Tom";


    }

}

例子2

Person

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:46
 */
public class Person {

    String name;
    int age;

    public Person(){
    }

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

    public void eat(){
        System.out.println("吃饭");
    }

    public void walk(int distance){
        System.out.println("走路,走的距离是:"+distance+"公里");
    }

}

student

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:39
 */
public class Student extends Person{

    String major;

    public Student(){

    }

    public Student(String major){
        this.major=major;
    }

    public void study(){
        System.out.println("学习,专业是:" + major);
    }

    public void eat(){
        System.out.println("学生应该多吃有营养的食物");
    }

}

PersonTest

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:49
 */
public class PersonTest {

    public static void main(String[] args) {
        Student s = new Student("计算机科学与技术");
        s.eat();
        s.walk(10);
        s.study();
    }

}


例子3

Person

package java3;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 17:41
 */
public class Person {

    String name;
    int age;
    int id=1001;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

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

    public void eat(){
        System.out.println("人:吃饭");
    }

    public void walk(){
        System.out.println("人,走路");
    }

}

Student

package java3;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 17:42
 */
public class Student extends Person{

    String major;
    int id=1002;

    public Student(){

    }

    public Student(String major) {
        this.major = major;
    }

    @Override
    public void eat() {
        System.out.println("学生,多吃有营养的食物");
    }

    public void study(){
        System.out.println("学生:学习知识");
        this.eat();
        super.eat();

    }

    public void show(){
        System.out.println("name = "+this.name+", age = "+this.age);
        System.out.println("id = " + this.id);
        System.out.println("id = " + super.id);
    }



}

SuperTest

package java3;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 17:42
 */
public class SuperTest {

    public static void main(String[] args) {

        Student student = new Student();

        student.study();



    }


}

例子4

Person

package java4;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:44
 */
public class Person {

    String name;
    int age;

    public void eat(){
        System.out.println("人,吃饭");
    }

    public void walk(){
        System.out.println("人,走路");
    }



}

Man

package java4;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:45
 */
public class Man extends Person{

    boolean isSmoking;

    public void earnMoney(){
        System.out.println("男人负责挣钱养家");
    }

    @Override
    public void eat() {
        System.out.println("男人多吃肉,长肌肉");
    }

    @Override
    public void walk() {
        System.out.println("男人霸气的走路");
    }
}

Woman

package java4;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:48
 */
public class Woman extends Person{

    boolean isBeauty;

    public void goShopping(){
        System.out.println("女人喜欢购物");
    }

    @Override
    public void eat() {
        System.out.println("女人少吃,为了减肥");
    }

    @Override
    public void walk() {
        System.out.println("女人窈窕的走路");
    }
}

PersonTest

package java4;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:49
 */
public class PersonTest {
    public static void main(String[] args) {

        Person p1 = new Person();
        p1.eat();

        Man man = new Man();
        man.eat();
        man.age=24;
        man.earnMoney();

        //***********************************************
        //对象的多态性,父类的引用指向子类的对象
        Person p2 = new Man();

        //多态的使用,当调用子父类同名参数的方法时,实际执行的是子类重写父类的方法----虚拟方法调用
        p2.eat();
        p2.walk();
        //Person p3 = new Woman();

        //多态的对象调用的方法必须是父类中也有的方法
//        p2.earnMoney();



    }

}

例子5

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 17:39
 */
public class AnimalTest {

    public static void main(String[] args) {
        AnimalTest test = new AnimalTest();
        test.func(new Dog());

        test.func(new Cat());

    }


    public void func(Animal animal){
        animal.eat();
        animal.shout();
    }

}


class Animal{

    public void eat(){
        System.out.println("动物,进食");
    }

    public void shout(){
        System.out.println("动物,叫");
    }

}

class Dog extends Animal{
    public void eat(){
        System.out.println("狗吃骨头");
    }

    public void shout(){
        System.out.println("汪汪汪");
    }
}

class Cat extends Animal{
    public void eat(){
        System.out.println("猫吃鱼");
    }

    public void shout(){
        System.out.println("喵!喵!喵!");
    }
}

例子6

Person

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:06
 */
public class Person {
    String name;
    int age;

    int id=1001;

    public void eat(){
        System.out.println("人,吃饭");
    }

    public void walk(){
        System.out.println("人,走路");
    }

}

Man

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:07
 */
public class Man extends Person{

    boolean isSmoking;

    int id=1002;

    public void earnMoney(){
        System.out.println("男人负责挣钱养家");
    }

    public void eat(){
        System.out.println("男人多吃肉,长肌肉");
    }

    public void walk(){
        System.out.println("男人霸气走路");
    }

}

Woman

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:09
 */
public class Woman extends Person{

    boolean isBeauty;

    public void goShopping(){
        System.out.println("女人喜欢购物");
    }

    public void eat(){
        System.out.println("女人少吃,为了减肥");
    }

    public void walk(){
        System.out.println("女人窈窕的走路");
    }

}

PersonTest

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:10
 */
public class PersonTest {

    public static void main(String[] args) {
        Person p2 = new Man();

        p2.eat();
        p2.walk();

        //p2.earnMoney();
        /*有了对象的多态性以后,内存中实际上是加载了子类特有的属性和方法,但是由于变量声明为父类类型,导致编译时,只能调用父类中声明的属性和方法,子类特有的属性和方法不能调用,如何才能调用呢?*/
        //向下转型
        Man m1 = (Man) p2;

        m1.earnMoney();
        m1.isSmoking=true;

        //这下面的肯定转不了
//        Woman w1 = (Woman) p2;
//
//        w1.goShopping();

    }

}

例子7

Person

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:06
 */
public class Person {
    String name;
    int age;

    int id=1001;

    public void eat(){
        System.out.println("人,吃饭");
    }

    public void walk(){
        System.out.println("人,走路");
    }

}

Man

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:07
 */
public class Man extends Person{

    boolean isSmoking;

    int id=1002;

    public void earnMoney(){
        System.out.println("男人负责挣钱养家");
    }

    public void eat(){
        System.out.println("男人多吃肉,长肌肉");
    }

    public void walk(){
        System.out.println("男人霸气走路");
    }

}

Woman

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:09
 */
public class Woman extends Person{

    boolean isBeauty;

    public void goShopping(){
        System.out.println("女人喜欢购物");
    }

    public void eat(){
        System.out.println("女人少吃,为了减肥");
    }

    public void walk(){
        System.out.println("女人窈窕的走路");
    }

}

PersonTest

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:10
 */
public class PersonTest {

    public static void main(String[] args) {
        Person p2 = new Man();

        p2.eat();
        p2.walk();

        //p2.earnMoney();
        /*有了对象的多态性以后,内存中实际上是加载了子类特有的属性和方法,但是由于变量声明为父类类型,导致编译时,只能调用父类中声明的属性和方法,子类特有的属性和方法不能调用,如何才能调用呢?*/
        //向下转型
        Man m1 = (Man) p2;

        m1.earnMoney();
        m1.isSmoking=true;


        //instanceof 关键字使用
        if(p2 instanceof Woman){
            Woman w1 = (Woman) p2;
            w1.goShopping();
            System.out.println("********Woman*************");
        }


        if(p2 instanceof Man){
            Man w1 = (Man) p2;
            w1.earnMoney();
            System.out.println("********Man*************");
        }

        if(p2 instanceof Person){
            System.out.println("*******Person***********");
        }

        if(p2 instanceof Object){
            System.out.println("**********Object************");
        }



    }

}

例子8

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:13
 */
public class EqualsTest {

    public static void main(String[] args) {

        int i=10;
        int j=10;
        double d= 10.0;
        System.out.println(i==d); //true

        char c=10;
        System.out.println(i==c);   //true

        char c1 = 'A';
        char c2 = 65;
        System.out.println(c1 == c2); //true

        Customer cust1 = new Customer("Tom", 21);
        Customer cust2 = new Customer("Tom", 21);

        System.out.println(cust1 == cust2);

        String str1 = new String("atguigu");
        String str2 = new String("atguigu");
        System.out.println(str1 == str2);
    }

}

例子9

package com.atguigu.java3;

import org.junit.Test;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:46
 */
public class WrapperTest {

    @Test
    public void test1(){

        int num1=10;
        Integer in1 = new Integer(num1);
        System.out.println(in1.toString());

        Integer in2 = new Integer("123");
        System.out.println(in2.toString());

        Float f1 = new Float(12.3f);
        Float f2 = new Float("12.3");
        System.out.println(f1);
        System.out.println(f2);


        Boolean b1 = new Boolean(true);
        System.out.println(b1);
        Boolean b2 = new Boolean("TrUe");
        System.out.println(b2);
        Boolean b3 = new Boolean("true123");
        System.out.println(b3);

        Order order = new Order();
        System.out.println(order.isMale);
        System.out.println(order.isFemale);

    }


}

class Order{
    boolean isMale;
    Boolean isFemale;
}

例子10

package com.atguigu.java3;

import org.junit.Test;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:46
 */
public class WrapperTest {

    @Test
    public void test2(){

        Integer in1 = new Integer(12);
        int i1 = in1.intValue();
        System.out.println(i1+1);

        Float f1 = new Float(12.3);
        float i2 = f1.floatValue();
        System.out.println(i2 + 1);

    }




}


例子11

package com.atguigu.java3;

import org.junit.Test;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:46
 */
public class WrapperTest {


    @Test
    public void test3(){

        //自动装箱:基本数据类型--->包装类
        int num2 = 10;
        Integer in1 = num2;
        System.out.println(in1);

        boolean b1 = true;
        Boolean b2 = b1;
        System.out.println(b2);

        //自动拆箱:包装类-->基本数据类型
        System.out.println(in1.toString());
        int num3 = in1;

        System.out.println(num3);


    }

}


例子12

package com.atguigu.java3;

import org.junit.Test;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:46
 */
public class WrapperTest {

    //基本数据类型,包装类--->String类型,调用String重载的valueOf(Xxx xxx)
    @Test
    public void test4(){

        //方式1:
        int num1=10;
        String str1 = num1 + "";
        System.out.println(str1);

        //方式2:
        float f1 = 12.3f;
        String str2 = String.valueOf(f1);
        System.out.println(str2);

        Double d1 = new Double(12.4);
        String str3 = String.valueOf(d1);
        System.out.println(str2);
        System.out.println(str3);

    }

    

}


例子13

package com.atguigu.java3;

import org.junit.Test;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:46
 */
public class WrapperTest {

    @Test
    public void test5(){
        String str1 = "123";

        //下面两个都是错误情况
//        int num1 = (int)str;
//        Integer in1 = (Integer) str1;

        int num2 = Integer.parseInt(str1);
        System.out.println(num2 + 1);

        String str2 = "true1";
        boolean b1 = Boolean.parseBoolean(str2);
        System.out.println(b1);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值