`

How do you know if an explicit object casting is needed

阅读更多

通俗来讲,不可能将一只是猫的动物强转为狗
再说Graphics2D:

 

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class MainClass {
	public static void main(String[] args) {
		JFrame jf = new JFrame("Demo");
		Container cp = jf.getContentPane();
		MyCanvas tl = new MyCanvas();
		cp.add(tl);
		jf.setSize(300, 200);
		jf.setVisible(true);
	}
}

class MyCanvas extends JComponent {
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Serif", Font.PLAIN, 96);
		g2.setFont(font);

		g2.drawString("jade", 40, 120);
	}
}

 

g之所以能强转为Graphics2D,多是因为它的运行时实例应是两者(Graphics,Graphics2D)的共同子类,当然Graphics2D本身是Graphics的子类,这里就像将一只是猫的动物强转为四脚兽

只有强转后,方可调用自Graphics2D特有的方法如setRenderingHint

另外一点只有向下转型才需强转(explicit casting),向上转型是自动完成的,比如当你将一个子类对象赋给一个父类型变量时。

 

from: http://topic.csdn.net/u/20110223/22/a00f64a0-c7ba-47fa-a4b1-4a703cc66df2.html?seed=1767655541&r=71791492#r_71791492

分享到:
评论

相关推荐

    GEE错误集-Cannot add an object of type <Element> to the map. Might be fixable with an explicit .pdf

    gee中出现下面问题的过程中的具体解决方案,总共有两种解决方案, Cannot add an object of type <Element> to the map. Might be fixable with an explicit cast to the correct type.

    Google C++ Style Guide(Google C++编程规范)高清PDF

    If an inline function definition is short, with very little, if any, logic in it, you should put the code in your .h file. For example, accessors and mutators should certainly be inside a class ...

    Top 11 Things you need to know about Devops

    1. What is DevOps and where did it come from? 2. How does DevOps differ from Agile? 3. How does DevOps differ from ITIL or ITSM? 4. How does DevOps fit with VisibleOps? 5. What are the unpinning ...

    IOS5 Programming Cookbook

    - **Explicit Casting**: Explicit casting is performed using `(type)` syntax. For example, `(id)object` casts `object` to `id`. - **Implicit Casting**: In ARC, implicit casting occurs when the compiler...

    前端开源库-is-explicit

    "is-explicit" 就是一个这样的开源库,它专注于显式性,旨在让对象和文本的操作更加直观和无缝。这个库的核心理念是通过运算符实例和类型运算符的组合,来提高代码的可读性和可维护性。 首先,我们要理解“显式”在...

    An explicit formula for the inverse of a pentadiagonal Toeplitz matrix

    As a result, an explicit inverse of a pentadiagonal Toeplitz matrix is obtained under certain assumptions. And numerical experiments are given to show the effectiveness of our results.

    Universal-USB-Installer

    and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions...

    WizFlow网页编辑

    you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that ...

    hibernate-shards.jar

    you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that ...

    vs版本管理工具最新版AnkhSvn-2.6.12735

    If your project is not in an existing workingcopy you are now asked where to store your project If some of the projects do not show glyphs Check if the projects are connected in File -> Subversion -> ...

    An end-to-end TextSpotter with Explicit Alignment and Attention

    An end-to-end TextSpotter with Explicit Alignment and Attention论文

    微软内部资料-SQL性能优化3

    For example, if T1 writes some object that is in T2’s input or output set, there is a dependency between T1 and T2. Bad Dependencies These include lost updates, dirty reads, non-repeatable reads, ...

    基于显式相关模型的贝叶斯分类器融合_Bayesian Classifier Fusion with an Explicit Mo

    基于显式相关模型的贝叶斯分类器融合(Bayesian Classifier Fusion with an Explicit Model of Correlation)是这一方法的一个具体实现,它考虑了分类器之间的相关性,从而提供了更精确的融合结果。 传统的分类器...

    Sakemail

    seems that some servers can‘t deglut the info too fast, causing problems with sockets buffers and leading to crash the client machine, I don‘t know if is a Borland bug or Microsoft bug. (thanks to ...

    How to build AutoML from scratch – Alexander Mamaev – Medium.pdf

    The WebML web modelling language is one example that does not yet rely on an explicit meta-model in the sense of MDE. Instead, it is defined in terms of a document type definition (DTD), and ...

    ASP.NET.Web.API.and.Angular.2.17864

    An explicit focus on understanding how the two tools fit together gives this book a unique insight on how to make SPAs for you and your clients that just work This book covers all the major topics you...

    MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数

    在MySQL 5.6中,`TIMESTAMP`数据类型和`explicit_defaults_for_timestamp`参数是数据库管理和时间戳处理的关键元素。`TIMESTAMP`通常用于记录数据记录的创建或修改时间,而`explicit_defaults_for_timestamp`参数则...

    Option Explicit

    Option Explicit Public txtSQL As String Dim mrc As adodb.Recordset '用户的读写权限标识 Dim mintRW As Integer '状态条中显示的时间信息 Public msBarText As String Public reportSQL As...

    C++ explicit通俗解释

    C++ explicit关键字在刚学的时候就接触到了,也从各处了解了一些,但始终云里雾里,在闲来无事的时候再仔细研究了一下,自己消化理解了一下,自认为比其他网友总结的精简而不失准确。

Global site tag (gtag.js) - Google Analytics