Skip to content

Instantly share code, notes, and snippets.

@aeris
Created November 4, 2011 20:59
Show Gist options
  • Save aeris/1340477 to your computer and use it in GitHub Desktop.
Save aeris/1340477 to your computer and use it in GitHub Desktop.
Héritage et polymorphisme
public class Main {
private static class A {
protected void foo() {
System.out.println("A");
}
}
private static class B extends A {
@Override
protected void foo() {
System.out.println("B");
}
}
public static void main(final String[] args) {
final A a = new A();
a.foo();
final B b = new B();
b.foo();
final A c = b;
c.foo();
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public new void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo {
public class Bar {
private class A {
public virtual void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public override void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
using System;
namespace Foo
{
public class Bar {
private class A {
public void Foo() {
Console.Out.WriteLine("A");
}
}
private class B : A {
public override void Foo() {
Console.Out.WriteLine("B");
}
}
public static void Main(string[] args) {
A a = new A();
a.Foo();
B b = new B();
b.Foo();
A c = b;
c.Foo();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment