파일 읽고 쓰기#

샘플소스

# 파일명(blog.py)
class Blog:
    id=1;
    value=2;

    def __init__(self):
        print 'Blog Object initialized';

    def __del__(self):
        print 'Blog Object destructed';

    def ha(self):
        return 'hahaha';

    def two(self):
        self.id+=2;
        self.value+=2;

    def three(self,num):
        self.id+=num;
        self.value+=num;

class NewBlog(Blog):
    def __init__(self):  # 생성자
        self.id*=10;
        print 'NewBlog Object initialized';

    def __del__(self):  # 소멸자
        print 'NewBlog Object destructed';

    def __repr__(self):   # 자바의 toString()과 같은 메서드
        return "object's value is %d %d" (self.id, self.id);

    def __cmp__(self):   # 자바의 compareTo와 같은 메서드
        if self.id>self.value:
            return -1;
        else:
            return 1;

    def end(self):
        print 'end';

아래처럼 객체 생성시 py파일명.class명() 형태로 객체를 생성할 수 있다.

import blog;

#b=blog.Blog();
b=blog.NewBlog();


print b.id;
print b.value;
print b.ha();

b.two();
print b.id;
print b.value;

b.three(3);
print b.id;
print b.value;

print b.end();
print b;

실행결과

NewBlog Object initialized
10
2
hahaha
12
4
15
7
end
None
object's value is 15 15
NewBlog Object destructed
[fromm0@server21010910266 class]$ vi classes.py
[fromm0@server21010910266 class]$ python classes.py
NewBlog Object initialized
10
2
hahaha
12
4
15
7
end
None
object's value is 15 15
NewBlog Object destructed

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-3) was last changed on 04-Oct-2008 19:41 by DongGukLee