JAVAoooooo
class Grandparent
{
public Grandparent()
{
System.out.println("GrandParent Created.");
}
public Grandparent(String string)
{
System.out.println("GrandParent Created.String:" + string);
}
}
class Parent extends Grandparent
{
public Parent()
{
//super("Hello.Grandparent."); System.out.println("Parent Created"); // super("Hello.Grandparent.");
}
}
class Child extends Parent
{
public Child()
{
System.out.println("Child Created");
}
}
public class TestInherits
{
public static void main(String args[])
{
Child c = new Child();
}
}

让我们意料之内的是,爷爷父亲儿子连续继承的时候,先调用爷爷的默认构造函数,在调用父亲的默认构造函数,在调用儿子的构造函数
以final声明的方法不允许覆盖。
以final声明的变量不允许更改。
利用final,可以设计出一种特殊的“只读” 的“不可变类”。
public class ExplorationJDKSource {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new A());
}
}
class A{}

此示例中定义了一个类A,它没有任何成员:直接输出这个类所创建的对象
我们发现打印了一个奇怪的东西
A@1279f2327
前面示例中,main方法实际上调用的是: public void println(Object x),这一方法内部调用了String类的valueOf方法。
valueOf方法内部又调用Object.toString方法: public String toString() { return getClass().getName() +"@" + Integer.toHexString(hashCode()); }
hashCode方法是本地方法,由JVM设计者实现: public native int hashCode();
在Java中,所有的类都派生自Object
Object是大爹是祖宗的祖宗
public class Fruit
{ public String toString()
{
return "Fruit toString.";
} public static void main(String args[])
{
Fruit f=new Fruit();
System.out.println("f="+f);
// System.out.println("f="+f.toString());
}
}

Fruit类覆盖了Object类的toString方法。构造函数返回的是一个字符串,然后对象就是调用toString
结论:
在“+”运算中,当任何一个对象与一个String对象,连接时,会隐式地调用其toString()方法,默认情况下,此方法返回“类名 @ + hashCode”。
为了返回有意义的信息,子类可以重写toString()方法。
class Parent
{
void message()
{
System.out.println("父亲的散文诗");
}
} class Child extends Parent
{ void message()
{
System.out.println("孩子的散文诗");
}
void test01()
{
super.message();
}
} public class TestInherits
{
public static void main(String args[])
{ Child c = new Child();
c.test01();
}
}
在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。
重写调用了父类的散文诗方法

public class TestInstanceof
{
public static void main(String[] args)
{ Object hello = "Hello"; System.out.println(" Object " + (hello instanceof Object)); System.out.println(" String " + (hello instanceof String)); System.out.println(" Math " + (hello instanceof Math)); System.out.println(" Comparable " + (hello instanceof Comparable));
String a = "Hello"; }
}

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}
public class TestCast
{
public static void main(String args[])
{
Mammal m;
Dog d=new Dog();
Cat c=new Cat();
m=d;
//d=m;
d=(Dog)m;//必须加强转换
//d=c;
//c=(Cat)m;
}
}
必须有强转换类型,把父类强转为子类
随机推荐
- MySQL执行函数时报错:Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation 'find_in_set'
执行函数时报错: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) f ...
- JS Leetcode 503. 下一个更大元素 II 题解分析,依旧单调栈做法解决此题
壹 ❀ 引 我在JS Leetcode 496. 下一个更大元素 I 更清晰的图解单调栈做法一文中,介绍了单调栈做法解决下一个更大元素的问题,比较巧的是这道题还有升级版,题目来自Leetcode503 ...
- Linux学习资料锦集
Linux 学习资料链接: (1)Linux常见命令及其用法_STM32李逼的博客-CSDN博客 (2)Linux命令了解_STM32李逼的博客-CSDN博客 3)Linux使用编辑器_STM32李 ...
- 基于keras的卷积神经网络(CNN)
1 前言 本文以MNIST手写数字分类为例,讲解使用一维卷积和二维卷积实现 CNN 模型.关于 MNIST 数据集的说明,见使用TensorFlow实现MNIST数据集分类.实验中主要用到 Conv1 ...
- Go语言的100个错误使用场景(48-54)|错误管理
目录 前言 7. 错误管理 7.1 panicking(#48) 7.2 不清楚何时应该包裹一个 error(#49) 7.3 检查错误类型不够精确(#50) 7.4 检查错误值不够精确(#51) 7 ...
- Oracle正则表达式实战
原文链接:http://oracle-base.com/articles/misc/regular-expressions-support-in-oracle.php Introduction Exa ...
- 为什么华为今年疯狂招od?
不知道的大家有没有发现 这两年市场不好公司用人需求紧缩 唯有华子疯狂招人 很多人都听过华为OD 但是具体是什么还是有很多人疑惑 总结以下三个部分: 1.为啥疯狂招od而不是之前的纯"外包&q ...
- [攻防世界][Reverse]xxxorrr
将目标文件拖入IDA 反汇编main函数 __int64 __fastcall main(int a1, char **a2, char **a3) { int i; // [rsp+Ch] [rbp ...
- Navicat 12连接mysql8.x报错2059 - authentication plugin 'caching_sha2_password' 解决办法
// %表示远程连接允许所有ip,如果只是连接本地,将%改为localhost即可 ALTER USER 'root'@'%' IDENTIFIED BY '你自己的mysql的密码' PASSWOR ...
- 51从零开始用Rust编写nginx,江湖救急,TLS证书快过期了
wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 负载均衡, 静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实 ...