标签:string 修改 test public 类的方法 cte 变化 void print
封装的定义:
为什么需要封装:
举例:
public class Human
{
private int age;
private String name;
public int getAge()
{
return age;
}
public void setAge( int age ) throws Exception
{
//封装age的检验逻辑,而不是暴露给每个调用者去处理
if( age > 120 )
{
throw new Exception( "Invalid value of age" );
}
this.age = age;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
}
Java的类可以分为三类:
在这三个类型之间存在如下关系:
继承以后子类可以得到什么:
关于构造函数:
类默认拥有无参构造函数,如果定义了其他有参构造函数,则无参函数失效,所以父类没有定义无参构造函数,不是指父类没有写无参构造函数。看下面的例子,父类为Human,子类为Programmer。
public class Human
{
//定义了有参构造函数,默认无参构造函数失效
public Human(String name)
{
}
}
public class Programmer
extends Human
{
public Programmer()
{
//如不显示调用,编译器会出现如下错误
//Implicit super constructor Human() is undefined. Must explicitly invoke another constructor
super( "x" );
}
}
为什么需要继承:
在了解多态之前,首先需要知道方法的唯一性标识即什么是相同/不同的方法:
何谓重载:
//重载的例子
public class Programmer
extends Human
{
public void coding() throws Exception
{
}
public void coding( String langType )
{
}
public String coding( String langType, String project )
{
return "";
}
}
//这不是重载,而是三个相同的方法,编译报错
public class Programmer
extends Human
{
public void coding() throws Exception
{
}
public void coding()
{
}
public String coding()
{
return "";
}
}
何谓覆盖/重写:
public class Human
{
public void coding( String langType )
{
}
}
public class Programmer
extends Human
{
//此方法为覆盖/重写
public void coding( String langType )
{
}
//此方法为上面方法的重载
public void coding( String langType, String project )
{
}
}
覆盖/重写的规则:
理解了上述知识点,是时候定义多态了:
上例子:
class Human
{
public void showName()
{
System.out.println( "I am Human" );
}
}
//继承关系
class Doctor
extends Human
{
//方法重写
public void showName()
{
System.out.println( "I am Doctor" );
}
}
class Programmer
extends Human
{
public void showName()
{
System.out.println( "I am Programmer" );
}
}
public class Test
{
//向上转型
public Human humanFactory( String humanType )
{
if( "doctor".equals( humanType ) )
{
return new Doctor();
}
if( "programmer".equals( humanType ) )
{
return new Programmer();
}
return new Human();
}
public static void main( String args[] )
{
Test test = new Test();
Human human = test.humanFactory( "doctor" );
human.showName();//Output:I am Doctor
human = test.humanFactory( "programmer" );
human.showName();//Output:I am Programmer
//一个接口的方法,表现出不同的形态,意即为多态也
}
}
向上转型的缺憾:
总结概括:
稍微复杂的例子:
class Human {
public void fun1() {
System.out.println("Human fun1");
fun2();
}
public void fun2() {
System.out.println("Human fun2");
}
}
class Programmer extends Human {
public void fun1(String name) {
System.out.println("Programmer‘s fun1");
}
public void fun2() {
System.out.println("Programmer‘s fun2");
}
}
public class Test {
public static void main(String[] args) {
Human human = new Programmer();
human.fun1();
}
/*
* Output:
* Human fun1
* Programmer‘s fun2
*/
}
package test;
class A {
public void func() {
System.out.println("func in A");
}
}
class B extends A {
public void func() {
System.out.println("func in B");
}
}
class C extends B {
public void func() {
System.out.println("func in B");
}
}
public class Bar {
public void test(A a) {
a.func();
System.out.println("test A in Bar");
}
public void test(C c) {
c.func();
System.out.println("test C in Bar");
}
public static void main(String[] args) {
Bar bar = new Bar();
A a = new A();
B b = new B();
C c = new C();
bar.test(a);
bar.test(b);
bar.test(c);
/*
func in A
test A in Bar
func in B
test A in Bar
func in B
test C in Bar
*/
}
}
标签:string 修改 test public 类的方法 cte 变化 void print
原文地址:https://www.cnblogs.com/JDBlog/p/10405677.html