Overloading and overriding method call

Overloading method will be determined by reference type, but override method will be determined by the actual object tyep.
property is determined by reference type too.

package newjob.guoliang.corejava;

public class ReferenceTypeTest {
	public static void main(String[] args) {
		AClass a = new BClass();
		new ReferenceTypeTest().printClassDesc(a);
	}

	protected void printClassDesc(AClass a) {
		System.out.println("A_" + a.desc);
	}

	protected void printClassDesc(BClass b) {
		System.out.println("B_" + b.desc);
	}

}

class AClass {
	String desc = "ClassA";
}

class BClass extends AClass{
	String desc = "ClassB";
}

Output is:

A_ClassA
MethodClassB_ClassB

This entry was posted in Java and tagged , , . Bookmark the permalink.

Java method invoked sequence

Static block / variable;

Instance block / variable;

Constructor;

public class StaticTest {
	public static void main(String[] args) {
		// System.out.println(B.a);
		// System.out.println(C.c);
		StaticTest staticTest = new StaticTest();
		staticTest.printName();
	}

	static {
		System.out.println("Static_block_Main");
	}

	public StaticTest() {
		System.out.println("Consturctor_Main");
	}

	private void printName() {
		System.out.println("InstanceMethod");
	}
	{
		System.out.println("Instance_block_Main");
	}

}

Output is:

Static_block_Main
Instance_block_Main
Consturctor_Main
InstanceMethod

This entry was posted in Java and tagged , . Bookmark the permalink.

About Java static block

package scjp.liguoliang.com;

/**
 * I'm try to see the static block run rules,
 * B extends A, A has a static a, when we use B.a, class B did not loaed, just Class A loaded.
 * If B also has a static a, A and B will be loaded.
 * If a is finle, A and B will not be loaded both.
 * @author Li Guoliang
 *
 */
public class TestClassLoading {
	public static void main(String[] args) {
		System.out.println(B.a);
		System.out.println(C.c);
	}
}

class A {
	static String a = "A.a"; // how about if this is final
	static {
		System.out.println("Static_A_Class");
	}

	{
		System.out.println("Instance_A");
	}
}

class B extends A{
	// static String a = "B.a";
	static {
		System.out.println("Static_B_Class");
	}
}

/**
 * This class is designed for test the squence of static variable and block.
 * They run by the sequence of the code.
 * @author Li Guoliang
 *
 */
class C {

	static {
		System.out.println("Static_C");
	}
	static C c = new C();

	public C() {
		System.out.println("Constructor_C");
	}
}

Output

Static_A_Class
A.a
Static_C
Constructor_C
scjp.liguoliang.com.C@161f10f

This entry was posted in Java and tagged , . Bookmark the permalink.

Which method will be called? about Overriding and Overloading in Java

Here are two class: Animal and Dog, Dog extends from Animal:

package scjp.liguoliang.com;

public class Animal {

	private String type = "Dog";
	public String name = "Animal";

	public String getType() {
		return type;
	}

	public void eat() {
		System.out.println(name + ", Animal eat...");
	}
}
package scjp.liguoliang.com;

public class Dog extends Animal {

	private String type = "Dog";
	public String name = "Dog";

	public String getType() {
		return type;
	}

	public void eat() {
		System.out.println(name + ", Dog eat...");
	}
}

And here is the test codes:

	public static void main(String[] args) {
		Dog dog = new Dog();
		Animal animalDog = new Dog(); // New dog, but type is Animal.

		System.out.println(dog.name);
		System.out.println(dog.getType());
		dog.eat();

		System.out.println(animalDog.name);
		System.out.println(animalDog.getType());
		animalDog.eat();

		System.out.println("\nWe are going to test overloading:");
		OverLoadingTest overLoadingTest = new OverLoadingTest();
		overLoadingTest.testEat(animalDog);
		overLoadingTest.testEat(dog);
	}

	public void testEat(Animal animal) {
		System.out.println("Test Animal eat");
	}

	public void testEat(Dog dog) {
		System.out.println("Test Dog eat");
	}

Here is the output:

Dog
Dog
Dog, Dog eat...
Animal // Get the property by reference Type, so print the name of ‘Animal’
Dog // Polymorphism, call the method of the instance in run time;
Dog, Dog eat...

We are going to test overloading:
Test Animal eat // Compiler will decide which method will be called by reference type when compiling. 
Test Dog eat

In summary:

1. overriding: Polymorphism is for instance method, so  an animal type reference to a dog Object will call dog’s method; but for properties, will use animals.

2. overloading: which method will be called has been determined when compiling by the reference type.

This entry was posted in Java and tagged , , , . Bookmark the permalink.

Configuring the Apache Web Server to Run Perl 在Apache中运行Perl

1. Install Perl

2. Install Apache & config httpd:

Options Indexes FollowSymLinks
Add ExecCGI to this line. The line should now look like the following:
Options Indexes FollowSymLinks ExecCGI
Next, search for the following:
#AddHandler cgi-script .cgi
Uncomment this line by removing the # in front of the line, and add a .pl to the end of the line. The new line should look like this:
AddHandler cgi-script .cgi .pl

3. Start the server, and check the server environment:

http://localhost:8080/cgi-bin/printenv.pl

There should be some  environment variables like  DOCUMENT_ROOT/PATH/SCRIPT_FILENAME….

4. Drop a Perl(hello.pl) into the server:

#!D:/perl/bin/perl.exe
print "Content-type: text/html; charset=utf-8\n\n";
print "<phtml><head><title>Learning Perl</title></head><body>";
print "<h1>PerlPage</h1>";
print "<form action='upload.pl' method='post'>";
print "<input type=file name=='file'>";
print "<input type=text name='comment' size=20 value=''>";
print "<input type=submit name=enter value='Upload'>";
print "<form>";
print "</body></html>";

you can just drop this file in your ApacheHome/cgi-bin or your site if you have configured the httpd.conf;

5. check the Perl page:

http://localhost:8080/cgi-bin/hello.pl

 

Ref:http://www.editrocket.com/articles/perl_apache_windows.html

This entry was posted in Perl and tagged , , , , . Bookmark the permalink.
Only 5 posts are displaying in this page, if you want to see more, click the categories navigation bar on the top or the tags on the right.