Classic ASP class property with inheritance
I'm having a serious brain fart as to why this isn't working for me. I have two classes
class Order
private Test_1
private oCustomer
public property get Test() Test= Test_1 end property
public property let Test( value ) Test_1 = value end property
public property get Customer()
if ( NOT isObject( oCustomer ) ) then
set oCustomer = new OrderCustomer
end if
set Customer = oCustomer
end property
end class
class OrderCustomer
private FirstName_1
public property get FirstName() FirstName = FirstName_1 end property
public property let FirstName( value ) FirstName_1 = value end property
end class
When I call the following code I get the result in the comments
set oOrder = new Order
oOrder.Test = "Hi"
response.write oOrder.Test() 'writes out "HI"
oOrder.Customer.FirstName = "Fred" 'does actually set it to this value, I am able to response.write out FirstName_1 after it is set in let
response.write oOrder.Customer.FirstName() 'writes out nothing
set oOrder = nothing
开发者_如何学C
What am I missing here? I was pretty sure I did this on previous projects.
The problem is that every time to access the Customer property it creates a new one
Proposed solution (not tested):
private _Customer
public property get Customer()
if _Customer is nothing then
set _Customer = new OrderCustomer
end if
set Customer = _Customer
end property
The property is always returning a new Customer. You should change order to something like
class Order
private m_Customer
private sub Class_Initialize()
set m_Customer = new Customer
end sub
public property get Customer()
set Customer = m_Customer
end property
end class
This will create a new customer object when an order is created.
Additionally, what is happening here isn't actually inheritance. Customer is just a property of the Order.
精彩评论