类class cast()方法 (Class class cast() method)

  • cast() method is available in java.lang package.

    在java.lang包中提供了cast()方法

  • cast() method casts this Object to the class or an interface denoted by this Class object.

    cast()方法将此Object强制转换为该Class或此Class对象表示的接口。

  • cast() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    cast()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • cast() method may throw ClassCastException at the time of casting an Object.

    cast()方法可能会在投射对象时抛出ClassCastException

    ClassCastException: In this exception when the given object is not null.

    ClassCastException :在此异常中,给定对象不为null。

Syntax:

句法:

    public Type cast(Object o);

Parameter(s):

参数:

  • Object o – represents the object to be cast.

    对象o –表示要投射的对象。

Return value:

返回值:

The return type of this method is Type, it returns the following values based on the given cases,

此方法的返回类型为Type ,它根据给定的情况返回以下值:

  • It returns the casting object.

    它返回转换对象。

  • It returns null when the given Object is null.

    当给定的Object为null时,它返回null。

Example:

例:

// Java program to demonstrate the example
// of Type cast(Object o) method of Class
class A1 {// A1 Blank implementation
}
class B1 extends A1 {// B1 Blank implementation
}
public class MainClass {public static void main(String[] args) {// Creting an instance of MainClass
MainClass mc = new MainClass();
// Display Class
System.out.println("mc.getClass():" + mc.getClass());
// Creating an instance of class A1 and B1
A1 a = new A1();
B1 b = new B1();
// Casting object b to a by using cast(b) method
Object a1 = A1.class.cast(b);
// Display Class of object a , b and a1
System.out.println("a.getClass(): " + a.getClass());
System.out.println("b.getClass(): " + b.getClass());
System.out.println("a1.getClass(): " + a1.getClass());
}
}

Output

输出量

mc.getClass():class MainClass
a.getClass(): class A1
b.getClass(): class B1
a1.getClass(): class B1

翻译自: https://www.includehelp.com/java/class-class-cast-method-with-example.aspx

Java类class cast()方法及示例相关推荐

  1. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  2. java 编写方法和属性,Java类属性及方法的定义

    文章导读 [在定义类时,经常需要抽象出它的属性和方法,并定义在类的主体中.本文探讨Java类属性及方法的定义和使用.通过本文的学习,你将了解如何在Java类中定义属性及方法.属性值的设置和获取.] 本 ...

  3. thymeleaf 调用java,thymeleaf模板引擎调用java类中的方法(附源码)

    前言 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去解决,有的是把问题留在项目的issue里提出,有的是在我的私人博客里留言,还有的则是直接添加我的qq来找我讲自己遇到 ...

  4. java散列法的运用实例,Java HashMap compute() 使用方法及示例

    Java HashMap compute() 使用方法及示例 Java HashMap compute()方法计算一个新值,并将其与哈希映射中的指定键相关联. compute()方法的语法为: has ...

  5. JSP中调用java类中的方法

    JSP中调用java类中的方法 1.新建一个项目,在src文件夹下添加一个包:如:test 2.再在包中添加一个类:如 package test; public class conDatabase { ...

  6. math的用法在java中的使用,Java Math cbrt() 使用方法及示例

    Java Math cbrt() 使用方法及示例 Java Math cbrt()方法返回指定数字的立方根. cbrt()方法的语法为: Math.cbrt(double num) 注意:cbrt() ...

  7. java arraylist 方法返回值,Java ArrayList get() 使用方法及示例

    Java ArrayList get() 使用方法及示例 Java ArrayList get()方法返回指定位置存在的元素. get()方法的语法为: arraylist.get(int index ...

  8. java kryo_Kryo框架使用方法代码示例

    Kryo框架的source已移至https://github.com/EsotericSoftware/kryo ,进入此页面,然后点击右边的Download Zip按钮,就能下载到最新版本的Kryo ...

  9. java readtimeout_Java HttpURLConnection.getReadTimeout方法代码示例

    import java.net.HttpURLConnection; //导入方法依赖的package包/类 /** * 得到响应对象 * * @param urlConnection * @retu ...

最新文章

  1. 搭建WordPress博客平台,云计算技术与应用实验报告
  2. AngularJS之Service(四)
  3. 查找所有的(字符相同但顺序不同) Find All Anagrams in a String
  4. 【NLP】在机器学习中开发情感分析器的5种方法
  5. MySQL高级in和not in
  6. JEPaaS笔记||学习培训资料||案例视频 【全套】
  7. 产品经理懂技术=流氓会武术(zz)
  8. 超分辨率技术,随机噪声
  9. 关于路由器操作系统的那点事儿
  10. webpack系列之-原理篇
  11. c语言程序设计967,2017年湖南师范大学数学与计算机科学学院967C语言程序设计和数据结构[专业硕士]之C程序设计考研仿真模拟题...
  12. 那些年的草根站长,都被时代淘汰了吗?
  13. android音乐播放器flac,五款Android手机FLAC,APE无损音乐播放器
  14. 联合分布(二):联合分布
  15. 连接数据线截图/截视频
  16. 极域教师端和学生端链接不上,出现这种问题怎么解决
  17. 网易云音乐Eason Chen 歌词词云
  18. clion设置为中文_手把手教你去除CLion的中文字符乱码
  19. 人脸扫描建模_人脸识别中的特征建模方法与流程
  20. 什么是通信协议?什么是互操作性?

热门文章

  1. python3.6安装ipython_centos6.5下安装python3.6、pip、ipython
  2. 电脑打字手指正确姿势_正确的弹琴手型,应该是怎样的?
  3. iphone怎样关闭副屏_小米新设计专利曝光:“Z”型折叠屏手机
  4. 【Java从入门到头秃专栏 】(三) 控制流程 Math Date DateFormat Calendar System BigDecimal Random
  5. LDAP命令介绍---dsreplication--initialize
  6. 【转】利用python的KMeans和PCA包实现聚类算法
  7. EventBus源码分析
  8. BABOK - 企业分析(Enterprise Analysis)概要
  9. VUE2 第五天学习--过渡效果
  10. 开源 免费 java CMS - FreeCMS2.1 菜单管理