ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

instance和类型转换+面向对象三大特性:强调一下+static关键字详解 生成随机数方法

2022-02-09 11:32:04  阅读:277  来源: 互联网

标签:类型转换 instanceof Person System instance static Student println out


instance和类型转换

#多态#

即同一方法可以根据发送对象的不同而采用多种不同的行为方式。

一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)

多态存在的条件 有继承关系

子类重写父类方法

父类引用指向子类对象

 

注意:多态是方法的多态,属性没有多态性。

instanceof (类型转换) 引用类型

 package com.oop.demo06;
 ​
 public class Person {
     public void run(){
         System.out.println("run");
    }
 }
 /*
         //Object-->String
         //Object-->Person-->Teacher
         //Object-->Person-->Student
         Object object = new Student();
 ​
         //System.out.println(X instanceof Y);//能不能编译通过!
 ​
         System.out.println(object instanceof Student);//true
         System.out.println(object instanceof Person);//true
         System.out.println(object instanceof Object);//true
         System.out.println(object instanceof Teacher);//false
         System.out.println(object instanceof String);//false
 ​
         System.out.println("================================");
         Person person = new Student();
 ​
         System.out.println(person instanceof Student);//true
         System.out.println(person instanceof Person);//true
         System.out.println(person instanceof Object);//true
         System.out.println(person instanceof Teacher);//false
         //System.out.println(person instanceof String);//编译就报错!!Person与String同级
 ​
         System.out.println("================================");
         Student student = new Student();
 ​
         System.out.println(student instanceof Student);//true
         System.out.println(student instanceof Person);//true
         System.out.println(student instanceof Object);//true
         //System.out.println(student instanceof Teacher);//编译就报错!
         //System.out.println(student instanceof String);//编译就报错!
  */
 ​
 package com.oop;
 ​
 ​
 import com.oop.demo06.Person;
 import com.oop.demo06.Student;
 ​
 //一个项目应该只存在一个main方法
 public class Application {
     public static void main(String[] args){
         //类型之间的转化: 基本类型转换 高低64 32 16 8 高到低:强转
         //类型之间的转化:父 子
 ​
         //高                   低
         Person person = new Student();
         //person.eat();//报错: 父类用不了子类的方法 需强制转换
         Student student = (Student)person;
         student.eat();//吃吃吃,就知道吃!
 ​
         System.out.println("=====================");
         //子类转父类可能会丢失一些本来的方法
         Student s = new Student();
         s.eat();
         Person person1 = s;
    }
 }
 /*
     1.父类引用指向子类的对象
     2.把子类转换为父类,向上转型;
     3.把父类转换为子类,向下转型;强制转换
     4.方便方法的调用,减少重复的代码!简介
 ​
     封装、继承、多态!   抽象类,接口
 ​
  */

面向对象三大特性:强调一下

封装 继承 多态

抽象: 编程思想! 持续的学习,茅塞顿开!多实践,多测试大脑中的想法!实践出真知

static关键字详解

 package com.oop.demo07;
 ​
 //Static
 public class Student {
 ​
     private static int age;//静态的变量
     private double score;//非静态变量
 ​
     public void run(){
         System.out.println("小狗在叫");
    }
 ​
     public static void go(){
         System.out.println("它跑起来了");
    }
 ​
     public static void main(String[] args) {
         go();//静态调静态
         new Student().run();//调普通方法(非静态方法)
    }
 }
 package com.oop.demo07;
 ​
 public class Person {
 ​
     /*{
         //代码块(匿名代码块)
     }
 ​
     static {
         //静态代码块
     }*/
     //2:赋初始值~
    {
         System.out.println("匿名代码块");
    }
     //1:静态代码块只执行一次~
     static{
         System.out.println("静态代码块");
    }
     //3.
     public Person() {
         System.out.println("构造方法");
    }
 ​
     public static void main(String[] args) {
         Person person1 = new Person();
         System.out.println("==================");
         Person person2 = new Person();
    }
 }
 package com.oop.demo07;
 ​
 //静态导入包~
 import static java.lang.Math.random;
 import static java.lang.Math.PI;
 ​
 ​
 public class Test {
 ​
     //Math.random() : 随机数
     public static void main(String[] args) {
         //System.out.println(Math.random());
         System.out.println(random());
         System.out.println(PI);
    }
 }

 

标签:类型转换,instanceof,Person,System,instance,static,Student,println,out
来源: https://www.cnblogs.com/wangshikang/p/15874296.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有